字符类型中的if / else语句

时间:2015-09-09 16:19:04

标签: java if-statement char

我想编写一个程序,输入1到4年的年代码和输出年级。

注意:使用if ... else语句和年份代码是字符类型。 (如果使用Array更好,我不允许使用数组,因为这是在条件控制结构中。在这之后,如何使用switch .... case语句编写这个程序?

如何声明char并从用户那里获取输入?

import java.util.Scanner;
import java.io.*;

public class CharStatement {

    public static void main(String[] a) {

    char userInput = new char;
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Enter year code: ");  
    System.out.println("");

    if (char == 1) {
    System.out.println("First Year");
    System.out.println("Freshmen");     
    }
    else if ( char == 2) {
        System.out.println("Second Year");
        System.out.println("Sophomore");            
    }
    else if (char == 3) {
        System.out.println("Third Year");
        System.out.println("Junior");   
    }
    else if (char == 4) {
        System.out.println("Fourth Year");
        System.out.println("Senior");   
    }
    else {
        System.out.println("Invalid");
    }

}

3 个答案:

答案 0 :(得分:1)

在java中接受单个字符有点乱。有关详细信息,请查看this

您可以使用def filteredStructure=structure.findAll{entry->entry.key.equalsIgnoreCase("2014") || entry.key.equalsIgnoreCase("2015")) 执行相同的操作,如下所示,作为代码中的修改,

int

此外,您无法在Java中将变量命名为System.out.println(""); int choice = keyboard.nextInt() // accepts an integer from the user if (choice == 1) { // check if the given input is equal to 1 System.out.println("First Year"); System.out.println("Freshmen"); } ,因为它是keyword

答案 1 :(得分:0)

  1. 如果您正在接受类似数字的输入,请使用整数类型而不是字符类型。
  2. 您已创建扫描仪类的对象,但忘记从键盘输入用户输入。
  3. 您不能将char用作变量名,因为它是关键字。
  4. 所以,修改后的代码使用if-else:

        import java.util.Scanner;
    
        public class IfStmt 
        {
             public static void main(String[] args) 
             {
                 Scanner keyboard = new Scanner(System.in);
                 System.out.print("Enter year code: ");  
                 int year=keyboard.nextInt();
                 System.out.println("");
    
                 if (year == 1) 
                 {
                     System.out.println("First Year");
                     System.out.println("Freshmen");     
                 }
    
                 else if ( year == 2) 
                 {
                     System.out.println("Second Year");
                     System.out.println("Sophomore");            
                 }
    
                 else if (year == 3) 
                 {
                     System.out.println("Third Year");
                     System.out.println("Junior");   
                 }
    
                 else if (year == 4) 
                 {
                     System.out.println("Fourth Year");
                     System.out.println("Senior");   
                 }
    
                 else 
                 {
                     System.out.println("Invalid");
                 }
            }
        }
    

    使用switch case修改代码:

    import java.util.Scanner;
    
    public class SwitchStmt 
    {
        public static void main(String[] args) 
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.print("Enter year code: ");  
            int year=keyboard.nextInt();
            System.out.println("");
    
            switch(year)
            {
                case 1:
                    System.out.println("First Year");
                    System.out.println("Freshmen");
                    break;
    
                case 2:
                    System.out.println("Second Year");
                    System.out.println("Sophomore");
                    break;
    
                case 3:
                    System.out.println("Third Year");
                    System.out.println("Junior");
                    break;
    
                case 4:
                    System.out.println("Fourth Year");
                    System.out.println("Senior");
                    break;
    
                default:
                    System.out.println("Invalid");
            }
        }
    }
    

答案 2 :(得分:0)

使用它可行。 ^ _ ^看到差异

import java.util.Scanner;

public class GradedExercise34
{
     public static void main(String[] args)
     {
         Scanner keyboard = new Scanner(System.in);
         System.out.print("Enter year code: ");
         char in=keyboard.next( ).charAt(0);
         System.out.println("");

         if (in == '1')
         {
             System.out.println("First Year");
             System.out.println("Freshmen");
         }

         else if ( in == '2')
         {
             System.out.println("Second Year");
             System.out.println("Sophomore");
         }

         else if (in == '3')
         {
             System.out.println("Third Year");
             System.out.println("Junior");
         }

         else if (in == '4')
         {
             System.out.println("Fourth Year");
             System.out.println("Senior");
         }

         else
         {
             System.out.println("Invalid");
         }
    }
}