如何制作一个不会重复输入两次的退出方法?

时间:2016-09-25 04:27:12

标签: java methods

我的问题是,我需要制作退出方法,转换方法和输入方法。问题是,我不知道如何制作一个包含入口和转换方法值的退出方法,而不会重复输入两次。

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.");
   }    
}

1 个答案:

答案 0 :(得分:1)

我会在Scanner中构建main,并将其传递给entry。接下来,我将从entry返回分钟数,并将其作为参数传递给conversion方法。最后,将minutesseconds传递给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);
}