简体   繁体   English

用字符串终止循环。 (JAVA)

[英]Terminating loops with strings. (Java)

Write a program that uses a while loop. 编写一个使用while循环的程序。 In each iteration of the loop, prompt the user to enter a number – positive, negative, or zero. 在循环的每次迭代中,提示用户输入一个数字-正,负或零。 Keep a running total of the numbers the user enters and also keep a count of the number of entries the user makes. 连续记录用户输入的数字,并保持用户输入的计数。 The program should stop whenever the user enters “q” to quit. 每当用户输入“ q”退出时,程序应停止。 When the user has finished, print the grand total and the number of entries the user typed. 用户完成操作后,打印总计和用户键入的条目数。

I can get this program to work when I enter a number like 0, to terminate the loop. 当我输入数字(如0)来终止循环时,我可以使该程序正常工作。 But I have no idea how to get it so that a string stops it. 但是我不知道如何获取它,以便字符串停止它。

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int count = 0;
    int sum = 0;
    int num;
    System.out.println("Enter an integer, enter q to quit.");
    num = in.nextInt();
    while (num != 0) {
        if (num > 0){
        sum += num;
        }
        if (num < 0){
        sum += num;  
        }
        count++;
        System.out.println("Enter an integer, enter q to quit.");
        num = in.nextInt();
    }
    System.out.println("You entered " + count + " terms, and the sum is " + sum + ".");

}

Your strategy would be to get the input as a string, check to see if it is a "q", and if not convert to number and loop. 您的策略是将输入作为字符串获取,检查它是否为“ q”,如果不转换为数字并循环。

(Since this is your project, I am only offering strategy rather than code) (由于这是您的项目,因此我仅提供策略而非代码)

This is the rough strategy: 这是粗略的策略:

String line;
line = [use your input method to get a line]
while (!line.trim().equalsIgnoreCase("q")) {
   int value = Integer.parseInt(line);
   [do your work]
   line = [use your input method to get a line]
}
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int count = 0;
    int sum = 0;
    String num;
    System.out.println("Enter an integer, enter q to quit.");
    num = in.next();
    while (!num.equals("q")) {
          sum += Integer.parseInt(num);
        count++;
      System.out.println("Enter an integer, enter q to quit.");
      num = in.next();
    }
    System.out.println("You entered " + count + " terms, and the sum is " + sum + ".");

  }

Cuts down on your code abit and is simple to understand and gives you exactly what you want. 减少代码使用量,易于理解,并为您提供所需的确切信息。

could also add an if statement to check if they entered another random values(so program doesn't crash if the user didn't listen). 还可以添加一条if语句来检查它们是否输入了另一个随机值(因此,如果用户不收听,则程序不会崩溃)。 Something like: 就像是:

if(isLetter(num.charAt(0))
  System.out.println("Not an int, try again");

Would put it right after the while loop, therefore it would already of checked if it was q. 将其放在while循环之后,因此将已经检查它是否为q。

java expects an integer but we should give the same exception. Java需要一个整数,但是我们应该给出相同的异常。 One way to solve this problem is entering a String, so that if the user first pressing is the Q, never enters the cycle, if not the Q. We assume that the user is an expert and will only enter numbers and the Q when you are finished. 解决此问题的一种方法是输入字符串,这样,如果用户首先按下的是Q,则不会输入循环(如果不是Q)。我们假设用户是专家,并且仅当您输入数字和Q时完成。 Within the while we convert the String to number with num.parseInt (String) 在一段时间内,我们将num.parseInt(String)转换为数字

Integer num;
String input;
while(!input.equal(q)){
 num=num.parseInt(input)
  if(num<0)
   sum+=1;
  else
   sumA+=1;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM