“Scanner inputs in java not functioning” can be translated to “Java中的Scanner输入不起作用.”

huangapple go评论56阅读模式
英文:

Scanner inputs in java not functioning

问题

我尝试创建一个简单的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]);
                }
            }
        }
    }
}

希望这可以帮助您解决代码中的问题。如果您有任何其他问题,请随时提问。

英文:

So i tried making a simple java reservation system but the problem is that the scanner functions won't work so the program is not taking any inputs. I tried putting the scanners but it shows me an warning about the local variables not being used when i tried to run the program and hence the scanners aren't working so the whole program is stuck on the input part and not doing anything else. Im pretty new to java and is trying to learn by creating projects so i will appreciate any help and advices thank you. Here is the code: So the part or line of code that shows a warning local variable not being used are lines 8,9,16,34,36,46,48 so far those are the only problems that are showing as well as the input or the scanners not working when i run the program.

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&lt;=5&amp;&amp;Regular&lt;=10)
{
System.out.print(&quot;Enter 1 for Premium class or 2 for Regular or -1 to exit&quot;);
Scanner in1 = new Scanner(System.in);
if(number == -1);
break;
}
if(number == 1 &amp;&amp; Premium &lt;= 5) {
System.out.println(&quot;You are assigned to First class\t&quot;+&quot;Seat#&quot; + Premium);
Premium++;
}
else if(number == 2 &amp;&amp; Regular &lt;=10) {
System.out.println(&quot;You are assigned to Regular class\t&quot; + Regular);
Regular++;
}
else if(number == 1 &amp;&amp; Premium &gt;= 5) {
System.out.println(&quot;First class full&quot;+&quot;  &quot;);
if(Regular &lt;= 10) {
System.out.println(&quot;Is it acceptable to be placed in the Regular class&quot;);
Scanner in1 = new Scanner(System.in);
in.close();
if(x.equals(&quot;y&quot;)) {
System.out.print(&quot;Seat Reserved in Regular class\t&quot; + Regular);
}
else {
System.out.println(&quot;Next Flight leaves in 3 hours.\t&quot;);
}
}
else if(number == 2 &amp;&amp; Regular == 10) {
System.out.println(&quot;Regular class full\t&quot;);
Scanner in1 = new Scanner(System.in);
in.close();
if(x.equals(&quot;y&quot;)) {
System.out.println(&quot;You are assigned to Premium\t&quot; + Premium);
}
else if (Premium == 5 &amp;&amp; Regular == 10) {
System.out.println(&quot;Plane is full, next flight is in 3&quot;);
}
else {
System.out.println(&quot;Next flight leaves in 3 hours\t&quot;);
}
for(int i=1; i&lt;planeclass.length; i++) {
System.out.println(planeclass[Premium] + planeclass[Regular]);
}
}
}
}			
}	

答案1

得分: 1

如果你想获取用户输入,那么你应该创建一个对象,然后使用该对象来接收输入。

示例:

Scanner input = new Scanner(System.in); //这里的input是对象的名称 int

x = input.nextInt(); //现在上面的表达式从用户那里获取一个整数作为输入
                       //nextInt()是Scanner类的方法

使用此教程查看Scanner类的所有方法。

英文:

If you want to take user input then you should create an object and then use that object to take input.

Example:


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

Use this tutorial to see all the methods of Scanner class

huangapple
  • 本文由 发表于 2020年8月14日 03:59:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63402421.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定