带有嵌套循环的Java if else

时间:2017-07-08 05:38:53

标签: java eclipse loops command-line-interface

我的java分配有一些问题,目前我被困在嵌套if else语句中。我实际上是试图从用户那里得到一个输入年龄并作为变量存储。执行我的代码后我得到错误当我运行程序时。我正确地执行此操作还是有其他方法来编程?

我收到的错误消息

    Enter your selection: 
    1
    You have selected No.1
    Please enter your age**Exception in thread "main" 
    java.lang.IllegalStateException: Scanner closed
    at java.util.Scanner.ensureOpen(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at Assignment.main(Assignment.java:48)**
public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    System.out.println(
       "1. Add player" + "\n" +
       "2.Display transaction" + "\n" +
       "3.Begin the ride");
    System.out.println("");
    System.out.println("Enter your selection: ");
    char selection = sc.findInLine(".").charAt(0);
    sc.close();
    switch(selection)  {
    case '1' :
        System.out.println("You have selected No.1");
        break;
    case '2' :
        System.out.println("You have selected No.2" );
        break;
    case '3' :
        System.out.println("You have selected No.3" );
        break;  
    default: 
        System.out.println("Please make a selection");
        break; 
   }
   if (selection=='1') {  
        int age=0;
        System.out.print("Please enter your age");
        age = sc.nextInt();
        while (age<100) {
            age+=age;
        }
    }       
    else if (selection=='2') {
        System.out.println("Display daily transaction");
    }
    else if (selection=='3') {
        System.out.println("Begin the ride");
    }
    else {
        System.out.println("Please enter a valid input");
    }
}

4 个答案:

答案 0 :(得分:1)

请阅读基本Java,以便更好地学习程序。

答案 1 :(得分:0)

switch case是一种嵌套if else.we可以使用switch来代替嵌套if else.you不需要同时使用switch case和嵌套if。 你可以这样写你的代码。

public static void main(String args[]) {

    Scanner sc = new Scanner(System.in);
    System.out.println("1. Add player" + "\n" +
       "2.Display transaction" + "\n" +
       "3.Begin the ride");
    System.out.println("");
    System.out.println("Enter your selection: ");
    char selection = sc.findInLine(".").charAt(0);

    switch(selection)  {

    case '1' :
        System.out.println("You have selected No.1");
        int age=0;
        System.out.print("Please enter your age");
        age = sc.nextInt();
        while (age<100) {
            age+=age;
        }
        System.out.print(age);
        break;

    case '2' :

        System.out.println("You have selected No.2" );
        System.out.println("Display daily transaction");
        break;

    case '3' :

        System.out.println("You have selected No.3" );
        System.out.println("Begin the ride");
        break;  

    default: 

        System.out.println("Please make a selection");
        System.out.println("Please enter a valid input");
        break; 

   }
    sc.close();
}

答案 2 :(得分:0)

你可以在很大程度上减少这些代码,我还没有删除嵌套if语句,你可以通过在case语句下移动所有执行来实现。

如果输入的选项不是数字,此程序将终止,这也可以通过将其作为字符串读取来进行改进,并且检查是其中一个有效选项(地图的键集)。

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;

public class SwitchCase {

    public static Map<Integer, String> options = new LinkedHashMap<Integer, String>();
    static {
        options.put(1, "Add player");
        options.put(2, "Display transaction");
        options.put(3, "Begin the ride");
    }

    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        for (Integer option : options.keySet()) {
            System.out.println(option + ". " + options.get(option));
        }
        System.out.println("Enter your selection: ");
        int selection = sc.nextInt();
        switch (selection) {
        case 1:
        case 2:
        case 3:
            System.out.println("You have selected No." + selection);
            break;
        default:
            System.out.println("Please make a selection");
            break;
        }
        if (selection == 1) {
            int age = 0;
            System.out.print("Please enter your age : ");
            age = sc.nextInt();
            while (age < 100) {
                age += age;
            }
            System.out.println(age);
        } else if (selection == 2) {
            System.out.println(options.get(selection));
        } else if (selection == 3) {
            System.out.println(options.get(selection));
        } else {
            System.out.println("Please enter a valid input!");
        }
    }
}

答案 3 :(得分:0)

问题非常明显:java.lang.IllegalStateException: Scanner closed

在您的代码进入switch之前,您需要使用sc.close();关闭扫描仪。但是后来你又试图再次阅读它:

if (selection=='1') {  
    int age=0;
    System.out.print("Please enter your age");
    age = sc.nextInt();  // <---- Here
    while (age<100) {
        age+=age;
    }
}  

但如果扫描仪已关闭,则会失败。要解决此问题,您只需将行sc.close()放在main的末尾即可。

作为替代方案,您可以将所有内容包装到try-with-resources块中,这样您就不必再关心扫描仪,因为它会在离开块后自动关闭。