gpt4 book ai didi

java - 如何制作一个不重复进入两次的退出方法?

转载 作者:行者123 更新时间:2023-12-01 09:28:01 26 4
gpt4 key购买 nike

我的问题是,我需要创建一个退出方法、一个转换方法和一个入口方法。问题是我不知道如何创建一个同时具有条目和转换方法值且条目不重复两次的退出方法。

import java.util.Scanner;

public class TimeConverter{

public static void main (String[]args){
System.out.println ("This program converts minutes to seconds.");
exit ();
}

public static int entry (){
System.out.print ("\nType in the amount of minutes:");
Scanner s = new Scanner (System.in);
int minutes = s.nextInt ();
return minutes;
}

public static int Conversion (){
int val1 = entry(),
cont = val1 * 60;
return cont;
}

public static void exit (){
int seconds = conversion (),
minutes = entry();
System.out.print ("\n\n" + minutes + " is equal to " + seconds + " seconds.");
}
}

最佳答案

我将在 main 中构造 Scanner,并将其传递给 entry。接下来,我将从 entry 返回分钟数,并将其作为参数传递给 conversion 方法。最后,将分钟传递给exit。就像,

public static void main(String[] args) {
System.out.println("This program converts minutes to seconds.");
Scanner s = new Scanner(System.in);
int minutes = entry(s);
int seconds = conversion(minutes);
exit(minutes, seconds);
}

public static int entry(Scanner s) {
System.out.println("Type a number of minutes:");
return s.nextInt();
}

public static int conversion(int minutes) {
return minutes * 60;
// Java also has built-in helpers for this, and you could use
// return (int) TimeUnit.MINUTES.toSeconds(minutes);
}

public static void exit(int minutes, int seconds) {
System.out.printf("%d minutes is equal to %d seconds.%n", minutes, seconds);
}

关于java - 如何制作一个不重复进入两次的退出方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39683367/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com