将扫描仪设置为全局变量[重复]

2024-03-21

顶部的方法是否将其声明为全局变量,因为它实际上并未接受任何内容。

 public class java_1 {

   static Scanner stdin = new Scanner(System.in);

import java.util.Scanner;

这就是用于声明它的代码,如果您可以通过其他方式声明它,我可以获得文档的链接。

public class java_1 {

   static Scanner stdin = new Scanner(System.in);

   static String getLastName (String Name){
       String lastName;
       int spacePos,length;

       spacePos = fullName.indexOf("");
       length = fullName.length();
       lastName = fullName.substring(spacePos + 1);
       return lastName;
   }


   static String getInitial (String fullName){
       String initial;
       initial = fullName.substring(0,1); 
       return initial;

   }
    static String =Name (){

       String fullName;
       String userName;
       String initial;
       String lastName;

          System.out.println("name");
          fullName = stdin.nextLine();

          initial = getInitial(fullName);
          lastName = getLastName(fullName);

          userName = initial + lastName;

          System.out.println(userName);
          return userName;
      }

static String printuserName (){

       String fullName,userName,initial,lastName;

          System.out.println("enter name");
          fullName = stdin.nextLine();

          initial = getInitial(fullName);

          userName = initial + lastName;

          System.out.println("username: " + userName);
          return userName;
      }


   static int menu(){

          int choice;
            System.out.println("Input a number from the table, corresponding to the task required");
            System.out.println("1 = User name");
            System.out.println("2 = Factor");
            System.out.println("3 = Quit");

            choice = stdin.nextInt();

            while (choice != 1){
                System.out.println("Re enter");
                choice = stdin.nextInt();
        }
            return choice;
   }




      }
     }
    }

问题是当你阅读菜单选项时stdin.nextInt(),扫描仪中仍有换行符等待。你打电话时stdin.nextLine()一段时间后,您将得到该行的其余部分 - 这只是一个空字符串。在以下调用之前,不会读取名称本身nextLine().

要解决此问题,您应该致电nextLine()在您致电后立即nextInt() in the Menu()方法,只是为了清除多余的换行符。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将扫描仪设置为全局变量[重复] 的相关文章

随机推荐