在java中使用while循环
我试图读入并累加(仅)正整数,直到输入负整数。仅当输入负整数时,程序才会停止。我能想到的最好的代码是下面的代码,但问题是即使读取负整数,它也不会停止。附言:我还在学习,所以请容忍我。我已尝试添加在java中使用while循环,java,loops,while-loop,Java,Loops,While Loop,我试图读入并累加(仅)正整数,直到输入负整数。仅当输入负整数时,程序才会停止。我能想到的最好的代码是下面的代码,但问题是即使读取负整数,它也不会停止。附言:我还在学习,所以请容忍我。我已尝试添加&&input2 您需要更改正在测试的变量,这里输入2,在循环的内部。否则就没有退出的机会。在循环中您所更改的是总变量,而input2保持不变,因此每次测试它时,它都保持>0,这就是您被卡住的原因。为什么不再试一次,这次更改input2(并且仍然使用input2更改total),看看您是否无法获得它。您在
&&input2 您需要更改正在测试的变量,这里输入2,在循环的内部。否则就没有退出的机会。在循环中您所更改的是总变量,而input2保持不变,因此每次测试它时,它都保持>0,这就是您被卡住的原因。为什么不再试一次,这次更改input2(并且仍然使用input2更改total),看看您是否无法获得它。您在循环中接受的数字没有存储到input2变量中。所以程序永远不会退出。顺便说一下,你甚至不需要输入2。变量input1就足够了。参见示例程序
下:
您的input1和input2被直接添加到某种缺陷中,目的是在第一次输入负数时退出,但我在下面显示的代码起作用。如果输入的数字为非负数,则仅提示用户输入任何其他数字;如果输入的数字为非负数,则仅对输入的数字求和
package stackoverflow.answers;
import java.util.Scanner;
public class Total {
public static void main(String[] args) {
int total = 0, input = 0;
/* Print out your "request" for an integer
* so the user knows to enter something.
*/
System.out.print("Enter an integer: ");
/* Create a Scanner on the System.in stream */
Scanner entry = new Scanner(System.in);
/* Loop while the entered number is > 0 */
while ((input = entry.nextInt()) > 0) {
/* If the input > 0, add it to total */
total += input;
/* Print out the request again. */
System.out.print("Total: " + total + "\nEnter an integer: ");
}
/* Just for kicks, print out the total
* (only useful if the first entered number
* is negative and the total would be 0). */
System.out.println("Total: " + total);
}
}
如果要将负数相加,然后退出,我建议将while循环更改为do-while循环,如下所示:
/* Get an integer and add it to the sum, and
* then loop while the entered number is > 0 */
do {
/* Get the integer */
input = entry.nextInt();
/* If the input > 0, add it to total */
total += input;
/* Print out the request again. */
System.out.print("Total: " + total + "\nEnter an integer: ");
} while (input > 0);
public类whileloop示例{
公共静态void main(字符串[]args){
int i=1;
System.out.println(“While循环演示”);
而
在最后一行中,您直接获取输入而不检查其正性,您直接将其添加到总数中。此外,在while循环中,您的条件是while(input2>0)
。假设您的input2得到一个正值,那么while循环将永远不会结束。因此,请使用下面的代码替换代码来进行更改
int input1 = entry.nextInt();
if(input1>0)
int total =input1;
while (input1 > 0){
System.out.println(total + "\nEnter another interger: ");
input1 = entry.nextInt();
if(input1>0)
total += input1;
}
布尔表达式,如真/假;
// Statements like System.out.prinln("Jai Shree Ram");
@满是鳗鱼的气垫船我完全同意。我错了。我不会给出答案。我没有看到你的答案。否则就不会有答案。过度促销特定产品/资源可能会被社区视为垃圾邮件。看看,特别是最后一节:避免公开的自我促销。你可能也对
public class WhileLoopExample {
public static void main(String[] args) {
int i=1;
System.out.println("While Loop Demo");
while(i<=10){
System.out.println(i);
i++;
}
}
}
int input1 = entry.nextInt();
System.out.println("Enter another integer: ");
int input2 = entry.nextInt();
int total = input1 + input2;
while (input2 > 0){
System.out.println(total + "\nEnter another interger: ");
total += entry.nextInt();
int input1 = entry.nextInt();
if(input1>0)
int total =input1;
while (input1 > 0){
System.out.println(total + "\nEnter another interger: ");
input1 = entry.nextInt();
if(input1>0)
total += input1;
}
while(Boolean_expression) {
// Statements
}
// Statements like System.out.prinln("Jai Shree Ram");