2013-08-04 41 views
3

我正在尝试读入并累加(仅)正整数,直到输入负整数。该程序应该只在输入负数时停止。我能够想到的最好的代码是下面的代码,但问题是即使读取了负数整数也不会停止。 PS:我还在学习,请耐心等待。我已经尝试将&& input2!<0添加到while循环条件,但它出现错误。有什么建议可以使我的行为符合我的要求?谢谢。在java中使用while循环

public class Total{ 
    public static void main(String[] args){ 

      System.out.println("Enter an integer: "); 

      Scanner entry = new Scanner(System.in); 
      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(); 
      } 
    } 
} 

回答

7

您需要更改内部循环的正在经受考验,在这里输入2变量,。否则就没有退出的机会。你在循环中改变的是总变量,而input2保持不变,所以每次测试时它都会保持为0,这就是为什么你被卡住了。为什么不再试试,这次改变了input2(并且仍然在改变total,使用input2),并且看看你是否无法获得它。

2

您在循环内接受的数字不会存储到input2变量中。所以程序永远不会退出。顺便说一句,你甚至不需要input2。变量input1就足够了。请参阅下面的示例程序 :

public static void main(String[] args){ 


      System.out.println("Enter an integer: "); 

      Scanner entry = new Scanner(System.in); 
      int input1 = entry.nextInt(); 


      int total =input1; 

      while (input1 > 0){ 
       System.out.println(total + "\nEnter another interger: "); 
       input1 = entry.nextInt(); 
       total += input1; 

      } 
    } 
+0

@Hovercraft Full Of Eels我完全同意。我的错。不会给出答案。我没有看到你的答案。否则就不会回答。 – Prem

4

你输入1和输入2中被直接加入一种缺陷戒烟上首次进入负数的目的,但我下面显示的代码工作。如果输入的数字是非负数,它只会提示用户输入任何附加数字,如果数字不是负数,它只会将输入的数字相加。

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); 
    } 
} 

如果你想求和负数,然后退出,我建议改变而循环到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); 
0
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++; 
     } 
    } 
} 
+0

特定产品/资源的过度推广可能会被社区视为垃圾邮件。看看[帮助中心](http://stackoverflow.com/help),特别是[用户期望什么样的行为?](http://stackoverflow.com/help/behavior)的最后一节:避免公开的自我推销。您可能还对[如何在堆栈溢出做广告?](http://stackoverflow.com/help/advertising)感兴趣。 – Draken

0
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(); 

在最后一行,您直接接受输入而不检查其正确性,您直接将其添加到总数中。此外,在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; 
       } 
0
while(Boolean_expression) { 
    // Statements 
} 

逻辑表达式像真/假作出改变;

// Statements like  System.out.prinln("Jai Shree Ram");