简体   繁体   English

将方法调用为主方法

[英]Calling Methods into the main method

i know this should be very simple but i cant seem to find my mistakes, i know that its a run-time error cause when i run the program it starts asks for what conversion you want to do then you enter the number and then an error occurs. 我知道这应该很简单,但是我似乎找不到我的错误,我知道它是运行时错误的原因,当我运行程序时,它会询问您要进行的转换,然后输入数字,然后输入错误发生。 We don't do very much trouble shooting in class but..... I've tried several different things, including 我们在课堂上拍摄时不会遇到很多麻烦,但是.....我尝试了几种不同的方法,包括

public static double hourstominutes(){
    double minutes, hours;
    Scanner input = new Scanner (System.in);

    System.out.println ("Enter number of hours:  ");
    hours = input.nextInt();
    input.close();

    minutes = (double)hours * (double)60;
    return minutes;

    }

but that didnt work, i think that made it worse becuse i would have to change a whole bunch of other lines. 但这没有用,我认为情况变得更糟,因为我必须更改其他许多行。

import java.util.Scanner;


public class TimeConverter{
public static void hourstominutes(){
    double minutes, hours;
    Scanner input = new Scanner (System.in);

    System.out.println ("Enter number of hours:  ");
    hours = input.nextDouble();
    input.close();

    minutes = (double)hours * (double)60;
    System.out.println("There are " + minutes + " minutes in " + hours + " hour(s");

    }
public static void minutestohours(){
    double minutes, hours;
    Scanner input = new Scanner (System.in);

    System.out.println ("Enter number of minutes:  ");
    minutes = input.nextDouble();
    input.close();

    hours = (double)minutes / (double)60;
    System.out.println("There are " + hours + " hours in " + minutes + " minutes");
}
public static void daystohours(){
    double days, hours;
    Scanner input = new Scanner (System.in);

    System.out.println("Enter number of day: ");
    days = input.nextDouble();
    input.close();

    hours = (double)days * (double)24;
    System.out.println("there are " + hours + " hours in " + days + "days");
}
public static void hourstodays(){
    double days; 
    double hours;
    Scanner input = new Scanner (System.in);

    System.out.println("Enter hours of day: ");
    hours = input.nextDouble();
    input.close();

    days = (double)hours / (double)24;
    System.out.println(days);
}
public static void main (String [] args){
    int option;
    Scanner input = new Scanner (System.in);

    System.out.println ("Please which conversion you would like to perform");
    System.out.println ("1. hours to minutes ");
    System.out.println ("2. minutes to hours");
    System.out.println ("3. days to hours");
    System.out.println ("4. hours to days");
    option = input.nextInt();
    input.close();

    if (option == 1){
        {hourstominutes();}
    }else if (option == 2){
        minutestohours();
    }else if (option == 3){
        daystohours();
    }else if (option == 4){
        hourstodays();
    }else {
        System.out.println("That is not a choice, enter a numerical value");
    }
}

}

i know that this is my homework and people say dont post hw but i have the whole thing done its just wrong and i dont know what to look for. 我知道这是我的作业,人们说不要发布硬件,但是我整个事情做错了,我也不知道要寻找什么。

thanks ahead of time for the help Stack over flow family 提前感谢您的帮助Stack over Flow系列

I think it is because when you close the input object, you close the System.in as well. 我认为这是因为当您关闭输入对象时,也会同时关闭System.in And when you use System.in to new a Scanner object again, you will get a java.util.NoSuchElementException . 并且当您再次使用System.in新建一个Scanner对象时,将得到一个java.util.NoSuchElementException It should be better if you use a static Scanner object and close it at the very end of your program. 如果使用静态Scanner对象并在程序的最后将其关闭,则效果会更好。 Hope this can help you. 希望这可以帮到你。

I've tested this and it works. 我已经对此进行了测试,并且有效。

Couple of things.. 几件事情..

1: Try not to make your methods all static, I know its asking you to, because you can't call a non static method from a static main, but that means you should instantiate your class, and use the object to access the methods. 1:尽量不要让您的方法全部都是静态的,我知道它要求您这样做,因为您不能从静态main调用非静态方法,但这意味着您应该实例化您的类,并使用该对象访问方法。

2: Scanner class uses an internal buffer. 2:扫描程序类使用内部缓冲区。 You create a Scanner object in the Factory class. 您在Factory类中创建一个Scanner对象。 This Scanner reads from the underlying FileInputStream into its own buffer. 该扫描程序从基础FileInputStream读取到其自己的缓冲区中。 the first Scanner has already consumed all the content in that FileInputStream. 第一个扫描器已经使用了该FileInputStream中的所有内容。

A quick fix is to use the same scanner object, (ie since all your methods are using a scanner, define the scanner in the class scope: 一种快速的解决方案是使用相同的扫描器对象,(即,由于您的所有方法都使用扫描器,因此请在类范围内定义扫描器:

Scanner input;
...
...
public static void main (String [] args){
input = new Scanner (System.in);
...
...

then just use input from all your methods, that will solve your problem. 然后只需使用所有方法的输入即可解决您的问题。 but do remember not to make all you methods static just cuz eclipse asked you. 但是请记住不要只是将所有方法设为静态,只是因为eclipse询问了您。 instantiate the class. 实例化该类。

good luck mate :) 祝你好运:)

Tip: try to use Scanner.nextLine() in your assignments, then parse the string to a double with Double.parseDouble() 提示:尝试在分配中使用Scanner.nextLine() ,然后使用Double.parseDouble()将字符串解析为双精度型

In your selection is easier if you use a switch statement selection = input.Text switch(selection) { case '1': hours(); 如果使用switch语句,则选择起来更容易。select = input.Text switch(selection){case'1':hours(); break; 打破;

etc 等等

default something(); 默认的something(); break } 打破

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM