Re: [問題] 如何加數字進陣列
假設 原來的 Pos method
是一個無良的SA開出來的
public static int Pos(int[] hw1);
那我會這樣子搞
public static void main(String[] args) {
System.out.println("輸入三個數字");
Scanner scn=new Scanner(System.in);
int[] hw1=new int[3];
for (int i=0;i<hw1.length;i++) {
hw1[i]=scn.nextInt();
}
int Pos = Pos(hw1);
String strPos = String.valueOf(Pos);
if (!"0".equals(strPos)) {
for (byte b : strPos.getBytes()) {
System.out.println("正數有:"+hw1[(Integer.valueOf(b) - 49)]);
}
} else {
System.out.println("輸入三個數字,沒有正數");
}
}
public static int Pos(int[] hw1){
String returnInt = "0";
for (int i=0;i<hw1.length;i++) {
if (hw1[i] > 0) {
returnInt = returnInt + String.valueOf(i+1);
}
}
return Integer.valueOf(returnInt);
}
--
假日被叫到公司加班很悶....
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 111.250.58.48
※ 編輯: ssc24 來自: 111.250.58.48 (10/12 17:23)
推
10/12 19:52, , 1F
10/12 19:52, 1F
→
10/12 19:54, , 2F
10/12 19:54, 2F
討論串 (同標題文章)
完整討論串 (本文為第 3 之 3 篇):
問題
1
4