如何让Java接受一些用户输入并将信息传递给某些方法?

时间:2015-11-02 22:37:05

标签: java

我正在尝试在Java中编写一个接受一些用户输入并将其传递给某些方法的main方法。

我的代码到目前为止:

//this method prints a menu to the console
public static void menu(){
    System.out.println("Select one of the following:\n");
    System.out.println("Enter Observatory Data[1]");
    System.out.println("Enter Earthquake Data[2]");
    System.out.println("Get Largest Ever Earthquake[3]");
    System.out.println("Get All Earthquakes Greater Than X[4]");
    System.out.println("Exit[5]");

}


public static void main(String[] args){
    Scanner reader= new Scanner(System.in);
    menu(); //print the menu to screen
    String input1=reader.next(); //user should select 1,2,3,4 or 5
    boolean cheese=false; //keep the program running
    while (cheese=false){
        if (input1.matches("[a-zA-Z67890]*")){ //if anything but 1,2,3,4,or 5 is entered return this string and reprint the menu
            //int inputNum1=Integer.parseInt(input1);
            System.out.println("no work!");
            menu();
            String input2=reader.next();
        }else if(input1.matches("1")){ //if user types 1 accept some information and pass it to the Observatory method
            System.out.println("What is the Observatory name?");
            String observatoryName = reader.next();
            System.out.println("What country is the observatory in?");
            String country = reader.next();
            System.out.println("What year did the observatory open??");
            String year = reader.next();
            System.out.println("Observatory added. Waht next?");
            System.out.println("What area does the observatory cover?");
            String area = reader.next();
            Observatory newObservatory = new Observatory(observatoryName,country,Integer.parseInt(year),Double.parseDouble(area));

还有其他一些选择,但只有粘贴一个就足够了。当前代码运行,打印菜单并接受一些用户输入,但是一旦程序终止,尽管剩余boolean cheese false。是否有人建议如何在输入选项5之前让Java运行并在选择1时检索某些信息?

2 个答案:

答案 0 :(得分:4)

在while循环条件下,您将cheese设置为false,而不是将cheese与false进行比较。将其更改为

while(!cheese) {

reader.next();

您还可以在上次实际输入后等待更多用户输入。所以在你最后一行之后,添加:

var gulp        = require('gulp');
var rename      = require('gulp-rename');
var gm = require('gulp-gm');
var originalBasename = '';

    gulp.task('resize-all', function () {
      return gulp.src(['_img/**/*.{jpg,png}', '_img/*.{jpg,png}'])
      .pipe(rename(function (path) {
        originalBasename = path.basename;
        path.basename += "-2048";
      }))
      .pipe(gm(function (gmfile) {
        return gmfile.resize(2048, 5000);
      }))
      .pipe(gulp.dest('_site/img'))
      .pipe(rename(function (path) {
        path.basename = originalBasename; // restore basename
        path.basename += "-1536";
      }))
        .pipe(gm(function (gmfile) {
          return gmfile.resize(1536, 5000);
        }))
        .pipe(gulp.dest('_site/img'))
        .pipe(rename(function (path) {
          path.basename = originalBasename; // restore basename
          path.basename += "-1080";
        }))
        .pipe(gm(function (gmfile) {
          return gmfile.resize(1080, 5000);
        }))
        .pipe(gulp.dest('_site/img'))
        .pipe(rename(function (path) {
          path.basename = originalBasename; // restore basename
          path.basename += "-750";
        }))
        .pipe(gm(function (gmfile) {
          return gmfile.resize(750, 5000);
        }))
        .pipe(gulp.dest('_site/img'))
        .pipe(rename(function (path) {
          path.basename = originalBasename; // restore basename
          path.basename += "-320";
        }))
        .pipe(gm(function (gmfile) {
          return gmfile.resize(320, 5000);
        }))
        .pipe(gulp.dest('_site/img'))
    });

以便程序在退出之前等待进一步的用户输入。如果我正确理解你的程序,你可以这样做,删除while循环,并达到预期的效果。

答案 1 :(得分:1)

您的状况不正确...... 它应该是:

boolean cheese = true;
while (cheese) 
{
    // Do stuff.
}

或者如果你真的想让你的奶酪布尔为假,那就这样做:

boolean cheese = false;
while (!cheese) 
{
    // Do stuff.
}