在交换机中使用用户输入

问题描述 投票:-1回答:1
import java.util.*;

public class assigment4number3
{
public static void main (String [] args)
  {


    int mon, tues, wed, thurs, fri;
    mon=1; tues=2; wed=3;thurs=4;fri=5;

    Scanner day = new Scanner(System.in);
    System.out.println("Enter Day of week: mon, tues, weds, thurs, fri, sat, sun");
   String week= day.next();


    switch (week) {
      case 1:
      System.out.println(" Discrete Math");
        System.out.println("Philosophy");
        System.out.println("Calculus");
       break;
      case 2:
       System.out.println("Modern World");
       System.out.println("Java Programming");
       break;
     case 3: 
        System.out.println(" Discrete Math");
        System.out.println("Philosophy");
        System.out.println("Calculus");
       break;
       case 4:
             System.out.println("Modern World");
       System.out.println("Java Programming");                 
       break;
      case 5:
       System.out.println("Discreet Math");
       System.out.println("Philosophy");
    break;
      default:
       System.out.println("Its the weekend");
    break;


    }
  }
  }

我正在尝试在我的交换机中使用输入我正在编写一个代码,我可以在一周中计算一天,我的日程安排应该弹出,但是我在交换机中使用输入时遇到了问题

java switch-statement
1个回答
0
投票

你所有的case标签都是int类型。将它们改为Stringswitch。喜欢,

int

要么

int week = day.nextInt();

如果您的意思是使用值switch (week) { case "1": System.out.println(" Discrete Math"); System.out.println("Philosophy"); System.out.println("Calculus"); break; case "2": System.out.println("Modern World"); System.out.println("Java Programming"); break; case "3": System.out.println(" Discrete Math"); System.out.println("Philosophy"); System.out.println("Calculus"); break; case "4": System.out.println("Modern World"); System.out.println("Java Programming"); break; case "5": System.out.println("Discreet Math"); System.out.println("Philosophy"); break; default: System.out.println("Its the weekend"); break; } - mon而不是数字,您可以将它们用于fri标签。喜欢,

case
© www.soinside.com 2019 - 2024. All rights reserved.