【问题标题】:Scanner inputs in java not functioning [duplicate]java中的扫描仪输入不起作用[重复]
【发布时间】:2020-12-03 17:32:35
【问题描述】:

所以我尝试制作一个简单的 java 预订系统,但问题是扫描仪功能无法工作,因此程序没有接受任何输入。我尝试放置扫描仪,但它向我显示当我尝试运行程序时未使用局部变量的警告,因此扫描仪无法工作,因此整个程序卡在输入部分而不做任何其他事情。我对 java 很陌生,正在尝试通过创建项目来学习,所以我将不胜感激任何帮助和建议,谢谢。这是代码:因此,显示警告局部变量未使用的部分或代码行是到目前为止的第 8、9、16、34、36、46、48 行,这些是唯一显示的问题以及当我运行程序时输入或扫描仪不工作。

package random.java;
import java.util.Scanner;
public class AirlineReservation {

    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);    
        int planeclass[] = new int[10];
        boolean available = false;
        boolean y=true;
        int number = 0, Premium = 1, Regular = 6;
        String x = null;
        
        while(Premium<=5&&Regular<=10)
        {
            System.out.print("Enter 1 for Premium class or 2 for Regular or -1 to exit");
            Scanner in1 = new Scanner(System.in);
            
                if(number == -1);
                break;
                
        }
        if(number == 1 && Premium <= 5) {
            System.out.println("You are assigned to First class\t"+"Seat#" + Premium);
            Premium++;
        }
        else if(number == 2 && Regular <=10) {
            System.out.println("You are assigned to Regular class\t" + Regular);
            Regular++;
        }
        else if(number == 1 && Premium >= 5) {
            System.out.println("First class full"+"  ");
            if(Regular <= 10) {
                System.out.println("Is it acceptable to be placed in the Regular class");
                Scanner in1 = new Scanner(System.in);
                in.close();
                if(x.equals("y")) {
                    System.out.print("Seat Reserved in Regular class\t" + Regular);
                }
                else {
                    System.out.println("Next Flight leaves in 3 hours.\t");
                }
            }
            
            else if(number == 2 && Regular == 10) {
                System.out.println("Regular class full\t");
                    Scanner in1 = new Scanner(System.in);
                    in.close();
                    if(x.equals("y")) {
                        System.out.println("You are assigned to Premium\t" + Premium);
                        
                    }
                    else if (Premium == 5 && Regular == 10) {
                        System.out.println("Plane is full, next flight is in 3");
                    }
                    else {
                        System.out.println("Next flight leaves in 3 hours\t");
                    }
                    for(int i=1; i<planeclass.length; i++) {
                        System.out.println(planeclass[Premium] + planeclass[Regular]);
                    }
            }
        }
    }           
}   

【问题讨论】:

标签: java


【解决方案1】:

如果您想接受用户输入,那么您应该创建一个对象,然后使用该对象接受输入。

示例:


Scanner input = new Scanner(System.in); //here input is the name of object int

x = input.nextInt(); //now the above expression take an integer as a input from user
                       //nextInt() is method of class Scanner

使用this tutorial查看Scanner类的所有方法

【讨论】:

  • 由于我的程序有很多输入,如何将它放在多行上?
  • 下次尝试将自己写的代码格式化为代码,也可以使用链接格式。
猜你喜欢
  • 2018-05-30
  • 1970-01-01
  • 1970-01-01
  • 2017-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-18
相关资源
最近更新 更多