当前位置: 首页 > 知识库问答 >
问题:

Java不允许我提示变量

阴高寒
2023-03-14

我正在创建一个程序来提示用户输入信息,所以我创建了多个变量,然后让程序根据这些变量输出信息。出于某种原因,每当我声明新的年龄变量时,程序都会给我一个奇怪的错误,在线程“main”中说Exception。我试着将第一次扫描关闭到,但不起作用。

我的密码是

package lab3;
import java.util.Scanner;

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

extract();

}

public static void extract () {

//variable declarations

     Scanner scan = new Scanner(System.in);
        System.out.println("First Name:  ");
        String firstname = scan.next();
        System.out.println("Last Name:  ");
        String lastname = scan.next();
        String output = firstname.substring(0, 1).toUpperCase() + firstname.substring(1) + " "
                + lastname.substring(0, 1).toUpperCase() + lastname.substring(1);
        


        // Input and processing section

        firstname = firstname.trim();
        lastname = lastname.trim();
        // output section

        Scanner scan1 = new Scanner(System.in);
        System.out.println("City and State :  ");
        String city = scan1.next();
        String state = scan1.next();
        scan1.close();
        
        Scanner scan2 = new Scanner(System.in);
        System.out.println("Age :  ");
        int age = scan2.nextInt();
        scan2.close();



        

        
     
    
        
        // output
        System.out.println("There was once a person named " + output + " who lived in " + city + " " + state + " After studying for 2 years at"
                + " a well-known college in " + state + " " +  firstname + " left the United states at age " + age);
    }}

共有1个答案

章博耘
2023-03-14

问题是,当你关闭一个物体扫描仪时,它就是底层系统。那是关着的。我已经对close()的调用进行了注释,您的脚本正在运行。

import java.util.Scanner;

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

extract();

}

public static void extract () {

//variable declarations

     Scanner scan = new Scanner(System.in);
        System.out.println("First Name:  ");
        String firstname = scan.next();
        System.out.println("Last Name:  ");
        String lastname = scan.next();
        String output = firstname.substring(0, 1).toUpperCase() + firstname.substring(1) + " "
                + lastname.substring(0, 1).toUpperCase() + lastname.substring(1);


        // Input and processing section

        firstname = firstname.trim();
        lastname = lastname.trim();
        // output section

        Scanner scan1 = new Scanner(System.in);
        System.out.println("City and State :  ");
        String city = scan1.next();
        String state = scan1.next();
//        scan1.close();

        Scanner scan2 = new Scanner(System.in);
        System.out.println("Age :  ");
        int age = scan2.nextInt();
 //       scan2.close();

        // output
        System.out.println("There was once a person named " + output + " who lived in " + city + " " + state + " After studying for 2 years at"
                + " a well-known college in " + state + " " +  firstname + " left the United states at age " + age);
    }}
 类似资料:
  • 我试图在我的汇总配置中设置一个SASS结构,允许我在整个应用程序中使用变量。我想用POSTSS自动刷新器。我在插件阵列中设置了以下内容: 这很好,我可以在我的组件中导入我的SCSS文件,比如,

  • 这个问题看起来像是重复的,但实际上不是。只是一个不断重复的微小差异。git一直告诉我:“请告诉我你是谁”,即使在设置之后。当我运行时,这就是我得到的…… 但是当我运行时,它会给我所有的详细信息… 我已经更改了我的姓名、电子邮件和代理,但当我运行命令时,它们看起来很好,即使在.gitconfig文件中,我也可以看到设置的值。可能缺少什么,因为我根本无法promise。每次它总是问我是谁? @sheu

  • 我一直在捕捉非数字时遇到问题。 我试过了,但抓不住。如果我让它捕获非数字,但不让用户再次尝试输入。。。它完全停止了我的代码。 这是我的密码:

  • 问题内容: 我有一个基于Java的Web服务客户端,该客户端连接到Java Web服务(在Axis1框架上实现)。 我的日志文件中出现以下异常: 问题答案: 这通常是由XML声明前的空白引起的,但是它可以是任何文本,例如破折号或任何字符。我说这通常是由空白引起的,因为人们认为空白始终是可忽略的,但事实并非如此。 经常发生的另一件事是UTF-8 BOM(字节顺序标记),如果将文档作为字符流传递给XM

  • 我把它放在php中。ini文件: 但我仍然可以调用它们(tested exec和shell_exec)。我已经多次重新启动Web服务器。 (窗户下)。

  • 这个问题是由打字错误或无法再复制的问题引起的。虽然这里可能有类似的问题,但这个问题的解决方式不太可能对未来的读者有所帮助。 原始关闭原因未解决 好的,我已经研究这个问题好几个小时了,不知道如何解决这个问题,因为我只是一个新手。当时我正在学习科尔特·斯蒂尔(Colt Steele)的一门名为WEBDEVBOOTCAMP的UDEMY课程。在部署部分,每次我使用Heroku部署时,这都会给我一个“不允许