java - 跳过代码

标签 java

问题出现在第七部分。它不允许我在某种状态下阅读,而是打印 else 语句。

import java.util.Scanner;

public class ifElseStatements
{
   public static Scanner in = new Scanner (System.in);

   public static void main (String[] args)
   {
      one();
      two();
      three();
      four();
      five();
      six();
      seven();
      eight();
   }
   public static void one()
   {
   System.out.print("grade: ");
   int number = in.nextInt();
   if(number > 70 && number <=100)
   {
      System.out.println("You're passing");
   }
   else
   {
      System.out.println("you're not passing!");
   }

   }  

   public static void two()
   {
      System.out.print("Number please");
      int b = in.nextInt();
      if(b <=50)
      {
      System.out.println("Go");
      }
      else
      {
      System.out.println("STOP");
      }
   }

   public static void three()
   {
      System.out.print("Integer please >");
      int c = in.nextInt();
      if(c%2 == 0)
      {
         System.out.println("Even");
      }
      else
      {
         System.out.println("odd");
      }
   }

   public static void four()
   {
      System.out.print("Integer please");
      int d = in.nextInt();
      if(d%5 == 0)
      {
         System.out.println("Multiple of 5");
      }
      else
      {
         System.out.println("Not a multiple of 5");
      }
   }

   public static void five()
   {
      System.out.print("number please");
      int e = in.nextInt();
      if(e< 10)
      {
         System.out.println("one digit");
      }
      else if(e>= 10 && e<100)
      {
         System.out.println("two digits");
      }
      else
      {
         System.out.println("three digits");
      }
   }

   public static void six()
   {
      System.out.print("Jersey Number");
      int f = in.nextInt();
      if(f == 12 || f == 80 || f == 71)
      {
         System.out.println("That number is retired from the seattle seahawks");
      }
      else
      {
         System.out.println("");
      }

   } 

   public static void seven()
   {
      System.out.print("a state is");
      String g = in.nextLine();
      if(g .equals ("Washington") || g .equals ("Oregon") || g .equals ("Idaho"))
      {
         System.out.println("That state is in the PNW!");
      }
      else
      {
         System.out.println("You should move to the PNW; Its great here!");
      }
   }  

   public static void eight()
   {
      System.out.print("drink size (SHORT, TALL, GRANDE, VENTI)");
      String h = in.nextLine();
      if(h .equals ("SHORT"))
      {
         System.out.println("8");
      }
      else if(h .equals ("TALL"))
      {
         System.out.println("12");
      }
      else if(h .equals ("GRANDE"))
      {
         System.out.println("16");
      }
      else if(h .equals ("VENTI"))
      {
         System.out.println("20");
      }
   }


}

这就是我运行代码时的样子。

grade: 70

you're not passing!

Number please12

Go

Integer please >30

Even

Integer please15

Multiple of 5

number please33

two digits

Jersey Number12

That number is retired from the seattle seahawks

a state isYou should move to the PNW; Its great here!

drink size (SHORT, TALL, GRANDE, VENTI)TALL

12 oz

最佳答案

上一步只是执行in.nextInt(),它读取下一个int,但不消耗EOL字符。因此,step7 方法读取下一行,并消耗上一步尚未消耗的 EOL。

您应该在第 6 步中添加 in.nextLine() 来使用 EOL 字符,而不仅仅是整数。

关于java - 跳过代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19622019/

相关文章:

java - Android 应用程序启动时不显示 GIF

java - BST删除方法不会删除插入的第一个节点

java - 无法使用for循环在java中初始化二维数组

java - 在 Java 中类型转换为字节

java - 如何在java中返回数组及其长度

Java从其他类创建ArrayList的实例

java - 如何从本地计算机使用 IAM 角色访问 S3

java - 将weblogic服务器添加到eclipse

Java while 循环/数学逻辑

c# - 与 Java 相比,C# 中的命名约定