简体   繁体   English

为什么这个 Java 程序可以在我的电脑上用 eclipse 编译,而在任何其他电脑上用 eclipse 和 JDK 都不能编译?

[英]Why does this Java program compile on my computer with eclipse, but not any other computer with eclipse and JDK?

So, for an assignment I'm supposed to do a couple different thing using scanners.因此,对于一项作业,我应该使用扫描仪做一些不同的事情。 I'm supposed to get 4 doubles as input and output the average of the 4, then its going to ask for two inputs(a first and last name), and then take the length, first letters, and print the name with the first letter capitalized, and then take the Sin and Cos of a degree entered, and it's up to me to code the conversion.我应该得到 4 个双打作为输入并输出 4 个的平均值,然后它会要求两个输入(名字和姓氏),然后取长度,第一个字母,并用第一个打印名称字母大写,然后取输入的学位的 Sin 和 Cos,由我来编码转换。

The problem I'm getting is that when I compile it on my computer it works perfectly fine, but when I submit it online, or just open it on another computer, it gives me an error: Exception in thread "main"我遇到的问题是,当我在我的计算机上编译它时它工作得很好,但是当我在线提交它,或者只是在另一台计算机上打开它时,它给了我一个错误:线程“main”中的异常

java.util.NoSuchElementException: **No line found
    at java.util.Scanner.nextLine(Scanner.java:1540)
    at Assignment2.main(Assignment2.java:39)**

I'm interpreting this as an error in line 39, which is bolded in my code below, but I have no idea what's actually wrong.我将此解释为第 39 行中的错误,在下面的代码中以粗体显示,但我不知道实际上是什么错误。

Haven't been able to go to my professors office hours because they're when i have a lab for a different class.没能去我教授的办公时间,因为那是我有一个不同班级的实验室的时候。

public static void main(String[] args){
    double in1;
    double in2;
    double in3;
    double in4;

    Scanner in = new Scanner(System.in);
    System.out.println("Input 4 numbers: ");
    in1 = in.nextDouble();
    in2 = in.nextDouble();
    in3 = in.nextDouble();
    in4 = in.nextDouble();
    in.nextLine();

    double average = (in1 + in2 + in3 + in4) / 4;
    System.out.print("The average of these four numbers is: "); 
    System.out.printf("%.2f" , average);
    System.out.println();

    System.out.println("What is your first name?: ");
        String firstName = in.nextLine(); //<----- this is where it tells me there is an error

    System.out.println("What is your last name: ");
        String lastName = in.nextLine();


    String firstAllCap = firstName.toUpperCase();
    String lastAllCap = lastName.toUpperCase();

    char first1 = firstAllCap.charAt(0);
    char last1 = lastAllCap.charAt(0);


    String firstNameCap = firstName.substring(0, 1).toUpperCase() + firstName.substring(1).toLowerCase();
    String lastNameCap = lastName.substring(0, 1).toUpperCase() + lastName.substring(1).toLowerCase();

    System.out.println("Your name is: " + firstNameCap + " " + lastNameCap);
    System.out.println("The first letter of the first name is: " + first1);
    System.out.println("The first letter of the Last name is: " + last1);
    int nameLength = firstName.length() + lastName.length();
    System.out.println("The length of your name is: " + nameLength);
    System.out.println("The intial letters are: " + first1 + last1);
    System.out.println("The last letter is: " + lastName.charAt(lastName.length()-1));
    System.out.println();

    System.out.println("Insert degree to evaluate cosine and sine: ");
    double degree = in.nextDouble();


    double radian = ((degree * Math.PI)/ 180);

    double sin = Math.sin(radian);
    double cos = Math.cos(radian);

    System.out.print("Sin(" + degree + "): ");
    System.out.printf("%.2f", sin);
    System.out.println();

    System.out.print("Cos(" + degree + "): ");
    System.out.printf("%.2f", cos);
    System.out.println();

    in.close();
}

When i compile on my computer this is the output:当我在我的电脑上编译时,这是输出:

Input 4 numbers:输入 4 个数字:

123
123
123
123

The average of these four numbers is: 123.00这四个数字的平均值是: 123.00

What is your first name?:你的名字是什么?:

matt

What is your last name:你姓什么:

kaminski

Your name is: Matt Kaminski你的名字是: Matt Kaminski

The first letter of the first name is: M名字的第一个字母是: M

The first letter of the Last name is: K姓氏的第一个字母是: K

The length of your name is: 12你名字的长度是: 12

The intial letters are: MK首字母是: MK

The last letter is: i最后一个字母是:我

Insert degree to evaluate cosine and sine:插入度数来计算余弦和正弦:

21

Sin(21.0): 0.36罪(21.0): 0.36

Cos(21.0): 0.93成本(21.0): 0.93

i just tested the code and it looks like it works fine.我刚刚测试了代码,看起来它工作正常。 however as per what i think it could be the way the online site provide inputs .然而,根据我的想法,这可能是在线网站提供输入的方式。

Maybe you should try to use in.next() instead of in.NextLine() and try to cast everything u want也许你应该尝试使用 in.next() 而不是 in.NextLine() 并尝试投射你想要的一切

it is possible that maybe the site is giving the input as 12 12 12 12 Test middle 50有可能该站点将输入作为 12 12 12 12 测试中间 50

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 eclipse是否有自己的JDK? 还是在我的计算机上使用了JDK? - does eclipse have it's own JDK? or it uses the JDK on my computer? 当我将工作空间加载到另一台计算机上时,为什么Eclipse会显示错误? - Why does Eclipse showing errors when I load my workspace on a different computer? 在eclipse中编译Java程序 - compile java program in eclipse 为什么我的Java程序在控制台而不在Eclipse中工作? - Why does my Java program work in console and not in Eclipse? 将Java项目(在Eclipse中)导出到另一台计算机 - Exporting a java project (in eclipse) to another computer Eclipse 冻结计算机 java 编程 android - Eclipse freezes computer java programming android 是否可以在Eclipse中使用Java程序来编译除Java之外的其他语言? - Is it possible to have a java program in eclipse to compile other languages than java? 为什么这个 2008 年的简单 Java 示例在我最新的 Eclipse/JDK 中失败? - Why does this simple Java example from 2008 fail in my up-to-date Eclipse/JDK? 为什么我的Eclipse插件显示在Eclipse RCP版本中,而不显示在Java和Java EE等其他版本中? - Why does my Eclipse plugin show up in Eclipse RCP version, but not in other versions like Java and Java EE? 新计算机,损坏的Eclipse - New computer, broken Eclipse
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM