调用输入字符串的数据

时间:2016-01-31 04:52:34

标签: java

我正在研究一个学校的课程,我遇到了一个错误,我很难解决。我检查了我的教科书,这个网站和其他网站,我遗漏了一些东西。

可能非常明显,有人请看下面的代码吗?

我从jGrasp收到以下错误:

  

错误:找不到符号   result = console.nextString()

/ 设计并实现在课程中实现星期几的课程日。  上课日应该存储一天,例如太阳周日。该程序应该能够执行  对Day"类型的对象进行以下操作;提供,今天,明天,昨天和未来的日期。在  这种情况下它将提供两周内的一天。还有一个测试程序的部分 /

这是我的代码:

import java.util.*;

public class Days2 {

    final static int sun = 0;
    final static int mon = 1;
    final static int tues = 2;
    final static int wed = 3;
    final static int thurs = 4;
    final static int fri = 5;
    final static int sat = 6;

    public int today;

    public Days2(int today) {
        this.today = today;
    }

    public void setDay(int today) {
        this.today = today;
    }

    public int getDay() {
        return today;
    }

    public int tomorrow() {
        if (today == sat) {
            return sun;
        } else {
            today = (today + 1) % 7;
        }
        return today;
    }

    public int yesterday() {
        if (today == 0) {
            return sat;
        } else {
            today = (today - 1) % 7;
        }
        return today;
    }

    public int twoWeeks(int todays) {
        return ((today + todays) - 1) % 7;
    }

    public String toString() {
        switch (this.today) {
            case sun:
                return "Sunday";
            case mon:
                return "Monday";
            case tues:
                return "Tuesday";
            case wed:
                return "Wednesday";
            case thurs:
                return "Thursday";
            case fri:
                return "Friday";
            case sat:
                return "Saturday";
        }
        return "";
    }

    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        String result;
        System.out.println("Please enter what day it is. (Sun, Mon, Tues, Wed, Thurs, Fri or Sat");
        result = console.nextString();

        //In order to test this program, enter a variable declared in the
        //beginning of the code instead of result into the object outDay listed
        //below.
        Days2 outDay = new Days2(result);
        System.out.print("Today is" + " " + outDay);
        System.out.println();
        outDay.setDay(outDay.yesterday());
        System.out.print("Yesterday was" + " " + outDay);
        System.out.println();
        outDay.setDay(outDay.tomorrow());
        outDay.setDay(outDay.tomorrow());
        System.out.print("Tomorrow is" + " " + outDay);
        System.out.println();
        outDay.setDay(outDay.twoWeeks(14));
        System.out.print("In two weeks it will be" + " " + outDay + " " + "again.");
        System.out.println();
    }
}

2 个答案:

答案 0 :(得分:1)

  

错误:找不到符号result = console.nextString()^

由于Scanner没有名为nextString()的方法。

Scanner doc

使用Scanner#next()读取字符串。

result = console.next();

但根据您的计划,您需要阅读int而不是String

所以从result更改intString的类型。

int result;

要阅读intScannernextInt()

result = console.nextInt();

将消息更改为

System.out.println("Please enter what day it is. "+
                  "(Sun-0, Mon-1, Tues-2, Wed-3, Thurs-4, Fri-5 or Sat-6");

答案 1 :(得分:1)

首先,接受字符串的正确语法是console.next()或console.nextLine()

你的程序的另一个问题是构造函数Days2接受一个int值作为参数,但是你用字符串作为参数调用它。

您可以将变量结果更改为int值,并将用于接受整数的命令从控制台更改为console.nextInt()