双开关方法疯了吗?

时间:2015-11-15 21:29:22

标签: java

这是方法类

import java.util.Scanner;

public class Methods extends Basic {
    Scanner keyboard = new Scanner(System.in);
    public void Nome(){
        String name = keyboard .nextLine();
       System.out.println("what is your name");
       switch(name){
       case "john":
           if(name.length() < 5){
               System.out.println("your name is average");
               break;
           }
       case "jason":
           if(name.length()>5){
               System.out.println("your name is not average");
               break;
           }
       }



    }
    public void Dates(){
          System.out.println("Enter a number and we will tell you what month it is:");
          int jay = keyboard.nextInt();
          switch(jay){
          case 1:
              System.out.println("january");
              break;
          case 2:
              System.out.println("February");
              break;
          case 3:
              System.out.println("March");
              break;
          case 4:
              System.out.println("April");
              break;
          case 5:
              System.out.println("May");
              break;
          case 6:
              System.out.println("June");
              break;
          case 7:
              System.out.println("July");
              break;

          default:
              System.out.println("that aint in the month dawg");

          }
    }
}

这是主要的课程

public class Basic  {

    public static void main(String args[])   {
        Methods dateo = new Methods();

        Methods nomalo = new Methods();
        nomalo.Nome();
        dateo.Dates();

    }
}

所以是的,我是一个完整的菜鸟。学习Java时我真的很挣扎:(很多开始学习java的朋友现在可以制作自己的2个游戏,而且我在这里挣扎着基本概念......

那么这段代码有什么问题?

2 个答案:

答案 0 :(得分:1)

编辑 - 忘掉我在这里说的话。你的代码工作正常。把你的 在进行扫描之前打印。

System.out.println("what is your name");
String name = keyboard.nextLine();

答案 1 :(得分:0)

因此,在打印“你叫什么名字”

之前,问题是你是否正在从键盘上读取数据

Methods类不需要扩展Basic。 name方法不需要有switch case。

所以Methods类看起来如下,

public class Methods {
    Scanner keyboard = new Scanner(System.in);

    public void name(){ 
       System.out.println("what is your name");
       String name = keyboard.nextLine();

       if(name.length() < 5){
               System.out.println("your name is average");
       } else {
              System.out.println("your name is not average");
       }
    }

    public void Dates(){
          System.out.println("Enter a number and we will tell you what month it is:");
          int jay = keyboard.nextInt();
          switch(jay){
          case 1:
              System.out.println("january");
              break;
          case 2:
              System.out.println("February");
              break;
          case 3:
              System.out.println("March");
              break;
          case 4:
              System.out.println("April");
              break;
          case 5:
              System.out.println("May");
              break;
          case 6:
              System.out.println("June");
              break;
          case 7:
              System.out.println("July");
              break;

          default:
              System.out.println("that aint in the month dawg");

          }
    }
}