這個問題在這里已經有了答案: 使用不同的最后分隔符連接字串 7 個答案 9 小時前關閉。
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("How old are you?");
int age = sc.nextInt();
System.out.println("What is your favorite number?");
int favnum = sc.nextInt();
int max, min;
String sequence ="";
if (favnum > age) {
max = favnum;
min = age;
} else {
max = age;
min = favnum;
}
System.out.print("The values between " max " and " min " that are divisible by 4 are ");
for (int i = max; i >= min; i--) {
if (i % 4 == 0) {
System.out.print(i ", ");
}
if(i % 4 == 0 && i / 4 == 1) {
System.out.print("and " i ".");
}
}
}
}
例如,如果輸入值介于 21 和 6 之間。我希望它列印“可被 4 整除的 21 和 6 之間的值是 20、16、12和 8。最后一位數字有一個和和一個句點結束。
這是我代碼的一小部分,但我一直堅持這部分我嘗試了很多方法來做到這一點,但它對我不起作用。
這是它列印的內容:
How old are you?
21
What is your favorite number?
6
The values between 21 and 6 that are divisible by 4 are 20, 16, 12, 8,
我想要它列印的內容:
The values between 21 and 6 that are divisible by 4 are 20, 16, 12, and 8.
uj5u.com熱心網友回復:
您需要以不同的方式執行此操作,并將所有可整除的數字添加到串列中,然后漂亮地列印串列,如下例所示:
public static void main(String[] args) {
int max = 20;
int min = 1;
List<Integer> divisibleBy4 = new ArrayList<>();
for (int i = max; i >= min; i--) {
if (i % 4 == 0) {
divisibleBy4.add(i);
}
}
for (int j = 0; j < divisibleBy4.size(); j ) {
if (j == 0) {
System.out.print(divisibleBy4.get(j));
} else if (j == divisibleBy4.size() - 1) {
System.out.print(" and " divisibleBy4.get(j) ".");
} else {
System.out.print(", " divisibleBy4.get(j));
}
}
}
將此添加到您的代碼中將產生以下作業代碼:
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("How old are you?");
int age = sc.nextInt();
System.out.println("What is your favorite number?");
int favnum = sc.nextInt();
int max, min;
String sequence ="";
if (favnum > age) {
max = favnum;
min = age;
} else {
max = age;
min = favnum;
}
System.out.print("The values between " max " and " min " that are divisible by 4 are ");
List<Integer> divisibleBy4 = new ArrayList<>();
for (int i = max; i >= min; i--) {
if (i % 4 == 0) {
divisibleBy4.add(i);
}
}
for (int j = 0; j < divisibleBy4.size(); j ) {
if (j == 0) {
System.out.print(divisibleBy4.get(j));
} else if (j == divisibleBy4.size() - 1) {
System.out.print(" and " divisibleBy4.get(j) ".");
} else {
System.out.print(", " divisibleBy4.get(j));
}
}
}
}
uj5u.com熱心網友回復:
您正在做的事情的困難在于,在開始回圈之前,您不知道序列中有多少個四的倍數。這使您難以處理這些情況:
不存在四的倍數
只有四的倍數(因此不需要“和”)
以不同方式處理最后一個倍數(如您所知)
為了解決這個問題,我會像這樣重構你的代碼(請原諒任何錯別字——我是用手機寫的)。
// figure out how many multiples of four exist within the range
int firstMultiple = max - (max % 4);
// handle no fours being present
if (firstMultiple < min) { ... }
int countFours = (firstMultiple - min) / 4;
countFours ; // add one since we started from a multiple
// loop for printing all but the last one
int stopAt = countFours - 1;
for (int i = 0; i < stopAt; i ) {
// print with commas inside this loop
int thisMultiple = firstMultiple - (4 * i);
}
if (countFours > 1) { /* print and here */ }
// print the final four
int finalFour = firstMultiple - (4 * stopAt);
uj5u.com熱心網友回復:
對于最后一個數字,您必須檢查 i 和 min 之間的差異是否小于 4 。因此,您可以在 for 回圈陳述句中添加以下內容。
public class Swtich {
public static void main(String[] args) {
System.out.println("How old are you?");
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
System.out.println("What is your favorite number?");
int favnum = sc.nextInt();
int max, min;
String sequence = "";
if (favnum > age) {
max = favnum;
min = age;
} else {
max = age;
min = favnum;
}
System.out.print("The values between " max " and " min " that are divisible by 4 are ");
StringBuilder sb = new StringBuilder();
for (int i = max; i >= min; i--) {
if (i % 4 == 0 && (i - min) <= 4) {
sb.delete(sb.length() - 2, sb.length());
sb.append(" and " i ".");
} else if (i % 4 == 0) {
sb.append(i ", ");
}
}
System.out.println(sb.toString());
}
}
輸出如下:
How old are you?
21
What is your favorite number?
6
The values between 21 and 6 that are divisible by 4 are 20, 16, 12 and 8.
uj5u.com熱心網友回復:
int counter=0; //add this counter
System.out.println("How old are you?");
int age = sc.nextInt();
System.out.println("What is your favorite number?");
int favnum = sc.nextInt();
int max, min;
String sequence ="";
if (favnum > age) {
max = favnum;
min = age;
} else {
max = age;
min = favnum;
}
System.out.print("The values between " max " and " min " that are divisible by 4 are ");
for (int i = max; i >= min; i--) {
if(i % 4==0) {
counter ; //Number of values that are divisible by 4.
}
}
for (int i = max; i >= min; i--) {
if(i % 4 == 0 && counter==1) {
System.out.print("and " i ".");
}else
{
if (i % 4 == 0) {
System.out.print(i ", ");
counter--;
}
}
}
這是輸出:
How old are you?
30
What is your favorite number?
4
The values between 30 and 4 that are divisible by 4 are 28, 24, 20, 16,
12, 8, and 4.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/419566.html
標籤:
下一篇:WebDriverException:訊息:服務geckodriver意外退出。狀態碼是:在FreeBSD監獄中使用SeleniumGeckodriverFirefox時出現64錯誤
