【问题标题】:Where to Properly Place the While-Do Statement?在哪里正确放置 While-Do 语句?
【发布时间】:2017-01-21 06:55:11
【问题描述】:

我不久前才开始编程,我真的很想学习更多。现在,我正在创建一个小的“if-else”、“while-do”场景,用户将使用正确的整数来回答问题。我遇到了麻烦,每当我点击这部分代码时:

if (chopperCrash > 100) {   


        System.out.println("That's not possible!");

        System.out.println("Hurry and re-enter a new value before we crash!");
        continue;

它不断循环打印的文本。有人可以在这个特定项目上帮助我,并向我详细解释“if-else”和“while-do”语句的正确位置吗?感谢您的任何帮助。如果这已经得到回答,我很抱歉,而且我太密集而无法理解其他先前提出的问题。这是我的完整代码块:

import java.util.Scanner;

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

    System.out.println("The chopper is going down because your lack of experience.");

    System.out.println("How much throttle are you going to give it?");

    System.out.println("(enter a number between 1 and 100)");

    Scanner FirstScan = new Scanner(System.in);


    int number = FirstScan.nextInt();
    int chopperCrash = number;

    while (true){       

    if (chopperCrash >= 60 && chopperCrash <= 100){

        System.out.println("Looks like we'll make it another day!");
        break;
    }

        else{       

    if (chopperCrash > 100) {   


        System.out.println("That's not possible!");

        System.out.println("Hurry and re-enter a new value before we crash!");
        continue;
    }
    else{
    if (chopperCrash <= 59) {
        System.out.println("Not enough throttle!");
        System.out.println("We're going down!");

        break;
             }
           }
         }
      }
   }
}

【问题讨论】:

    标签: java if-statement while-loop do-while


    【解决方案1】:

    您将输入代码排除在循环之外:

    int number = FirstScan.nextInt();
    int chopperCrash = number;
    
    while (true) {
    

    由于您从未在循环内分配chopperCrash,因此它会一直持续下去。所以,像这样放在里面:

    while (true) {
        int number = FirstScan.nextInt();
        int chopperCrash = number;
    

    【讨论】:

      猜你喜欢
      • 2020-05-07
      • 1970-01-01
      • 2016-01-10
      • 1970-01-01
      • 1970-01-01
      • 2015-08-16
      • 2016-04-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多