节目“日”75%工作......帮助JAVA

时间:2013-12-23 21:11:28

标签: java class

75%的代码都有效,这部分适用于我的任务...我已经获得了部分a-e

以下我无法使用该部分代码运行...

  • F。通过在当前添加特定天数来计算并返回当天 天。例如,如果当天是星期一,我们会增加四天, 返回的日子是星期五。同样,如果今天是星期二和 我们添加了13天,返回的日期是星期一。
  • -G。添加适当的构造函数。

有关如何构建我编写的内容的任何建议,以便用第一部分进行计算吗?

import java.util.*;
/* 
Personal information
*/
public class MyDayIIII
{
static Scanner readinput = new Scanner(System.in); //Creates common input object class
String d1;                                     // Private member variable 

public MyDayIIII(String d1)                //Constructor of value Sunday
{
d1 = "Sunday";
}
public void setD1(String theD1)           //Assigns value for user's input for "set day"
{
d1 = theD1;
}
public String claimD1()                   //Displays user's day
{
return d1;
}
// print the day, looks unnecessary
public void printD1()
{
System.out.println("Printed Day: " + d1);
}
// return the next day
public String tomD1()
{



// using direct comparison will fail as day == "Saturday"?
// so use compareTo() method...
if(d1.compareTo("Saturday") == 0)
return ("Sunday");
else if(d1.compareTo("Sunday") == 0)
return ("Monday");
else if(d1.compareTo("Monday") == 0)
return ("Tuesday");
else if(d1.compareTo("Tuesday") == 0)
return ("Wednesday");
else if(d1.compareTo("Wednesday") == 0)
return ("Thursday");
else if(d1.compareTo("Thursday") == 0)
return ("Friday");
else if(d1.compareTo("Friday") == 0)
return ("Saturday");
else
return ("\"Please Capitalize the First Letter of Your Day(e.g. Monday)\"");
}
// return the previous day
public String SetprD1()
{
if(d1.compareTo("Friday") == 0)
return ("Thursday");
else if(d1.compareTo("Thursday") == 0)
return ("Wednesday");
else if(d1.compareTo("Wednesday") == 0)
return ("Tuesday");
else if(d1.compareTo("Tuesday") == 0)
return ("Monday");
else if(d1.compareTo("Monday") == 0)
return ("Sunday");
else if(d1.compareTo("Sunday") == 0)
return ("Saturday");
else if(d1.compareTo("Saturday") == 0)
return ("Friday");
else
return ("\"Please Capitalize the First Letter of Your Day(e.g. Monday)\"");
}

public static void main (String args[])
{
MyDay testday = new MyDay("Sun");
System.out.print("Enter A Day(First Letter Capitalized & Full Spelling) ");
// read and store user's day
String storeday = readinput.nextLine();
// invoke setDay() method to set a day defined by user
testday.setDay(storeday);
// invoke getDay() method to get a day
System.out.println("You Chose: " + testday.getDay());
// test printing by invoking printDay() method
testday.printDay();
// invoke getPreDay() method to get the previous day
System.out.println("Day Prior Is: " + testday.getPreDay());
// invoke getNextDay() method to get the next day
System.out.println("The Following Day Is: " + testday.getNextDay()); 
}
}

我一直试图插入它,让它做部分F.

它会开始,但我得到以下......

at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:449)
    at java.lang.Integer.valueOf(Integer.java:554)
    at TextCalculator.main(TextCalculator.java:24

所以我把它拿出原来的......任何人都可以给我任何反馈,我感到很沮丧...

我的目标是创建一个包含7个元素的数组,然后使用7的模数计算未来日期,并使其对应于元素的值......

String[] day = new String[7];

day[0] = "Monday";                  //Elements were assigned the day of the week for their    value
day[1] = "Tuesday";
day[2] = "Wendesday";
day[3] = "Thursday";
day[4] = "Friday";
day[5] = "Saturday";
day[6] = "Sunday";

Scanner scanner = new Scanner(System.in);

System.out.println("Enter a Number for Future Days:");     //Prompting user to input number of days

String future = scanner.nextLine();

int futureDay = Integer.valueOf("future");

int numDay = day.length % 7;     //Using value of elements, with modulus 7 to assign value     and determine placement

while (numDay >= day.length)     
{
numDay -= day.length;
}

1 个答案:

答案 0 :(得分:2)

看起来像你的代码

int futureDay = Integer.valueOf("future");

什么时候应该

int futureDay = Integer.valueOf(future);

注意未来没有报价。这是因为“future”是一个字符串,其中包含字符f,u,t,u,r,e(显然不是整数),其中future是您之前声明的变量的名称。