I am trying to make a program that gets the day from the user input and then tells them the day before and the day after.The user should also be able to input how many days to add and the program should output that day.
example user enters 1 = Monday, tomorrow is = 2 Tuesday yesterday was = 3 Sunday
if user says its Monday(1) and adds 12 days the output should be Saturday(6)
The problem is whenever "theWeekDay" is greater than 7 it outputs nothing because TheDay(); doesn't have a condition for something greater than 7. Please help me!
Thanks you so much!
import java.util.Scanner;
import java.util.Scanner;
public class Problem_3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int theWeekDay;
System.out.println("What Day Is It?");
theWeekDay = input.nextInt();
Days one = new Days(theWeekDay);
System.out.println("Today It Is: ");
one.TheDay(theWeekDay);
System.out.println("Yesterday It Was: ");
one.PreviousDay(theWeekDay);
System.out.println("Tomorrow It Is: ");
one.NextDay(theWeekDay);
System.out.println("How Many Days To Add?");
int x = input.nextInt();
System.out.println("Now It Is: ");
one.AddedDays(x);
}
}
class Days {
private int theWeekDay;
public Days(int theWeekDay) {
this.theWeekDay = theWeekDay;
}
public int getTheWeekDay() {
return theWeekDay;
}
public void setTheWeekDay(int theWeekDay) {
this.theWeekDay = theWeekDay;
}
public int TheDay(int theWeekDay) {
// an arra days of week + then add days in it
if (theWeekDay == 0) {
theWeekDay = theWeekDay + 7;
}
if (theWeekDay == 1) {
System.out.println("Monday");
} else if (theWeekDay == 2) {
System.out.println("Tuesday");
} else if (theWeekDay == 3) {
System.out.println("Wednsday");
} else if (theWeekDay == 4) {
System.out.println("Thursday");
} else if (theWeekDay == 5) {
System.out.println("Friday");
} else if (theWeekDay == 6) {
System.out.println("Saturday");
} else if (theWeekDay == 7) {
System.out.println("Sunday");
}
return theWeekDay;
}
public int PreviousDay(int theWeekDay) {
theWeekDay = theWeekDay - 1;
return TheDay(theWeekDay);
}
public int NextDay(int theWeekDay) {
theWeekDay = theWeekDay + 1;
if (theWeekDay > 7) {
theWeekDay = 1;
}
return TheDay(theWeekDay);
}
public int AddedDays(int AddedDays) {
getTheWeekDay();
theWeekDay = theWeekDay + AddedDays;
return TheDay(theWeekDay);
}
}
If you want to assume value greater that 7 as valid, you should definitely use modulo operation. Something like this:
if(theWeekDay > 7) {
theWeekDay = theWeekDay % 7;
}
Otherwise you should throw and exception.
your if else must cover all cases ....
add an else after
else if(theWeekDay == 7){
System.out.println("Sunday");
}
something like:
if(theWeekDay == 1){
System.out.println("Monday");
}else if(theWeekDay == 2){
System.out.println("Tuesday");
}else if(theWeekDay == 3){
System.out.println("Wednsday");
}else if(theWeekDay == 4){
System.out.println("Thursday");
}else if(theWeekDay == 5){
System.out.println("Friday");
}else if(theWeekDay == 6){
System.out.println("Saturday");
}else if(theWeekDay == 7){
System.out.println("Sunday");
}else{
System.out.println("Invalid input");
}
return theWeekDay;
As user629735 said, use modulo in your formula.
public int AddedDays(int AddedDays) {
getTheWeekDay();
theWeekDay = (theWeekDay + AddedDays) % 7;
return TheDay(theWeekDay);
}
Related
I am relatively new to Java, and I am writing a simple program to play rock, paper, scissors. This is the code:
import java.util.Random;
import java.util.Scanner;
public class Main {
public static String comChoice() {
Random rand = new Random();
int num = rand.nextInt(299) + 1;
if (num >= 1 && num <= 99) {
String pick = "SCISSORS";
} else if (num >= 100 && num <= 199) {
String pick = "ROCK";
} else if (num >= 200 && num <= 299) {
String pick = "PAPER";
}
return pick;
}
public static void main(String args[]) {
int wins = 0;
int losses = 0;
int ties = 0;
while (1 == 1) {
Scanner s = new Scanner(System.in);
System.out.println("Would you like to play Rock Paper Scissors? Y/N");
String n = s.next();
if (n.equalsIgnoreCase("Y")) {
Scanner t = new Scanner(System.in);
System.out.println("Enter your pick: rock, paper, or scissors");
String userChoice = t.next();
String pick = comChoice();
if (userChoice.equalsIgnoreCase("SCISSORS") && pick.equalsIgnoreCase("SCISSORS")) {
System.out.println("TIE");
ties++;
} else if (userChoice.equalsIgnoreCase("ROCK") && pick.equalsIgnoreCase("SCISSORS")) {
System.out.println("WIN");
wins++;
} else if (userChoice.equalsIgnoreCase("PAPER") && pick.equalsIgnoreCase("SCISSORS")) {
System.out.println("LOSE");
losses++;
} else if (userChoice.equalsIgnoreCase("SCISSORS") && pick.equalsIgnoreCase("ROCK")) {
System.out.println("LOSE");
losses++;
} else if (userChoice.equalsIgnoreCase("SCISSORS") && pick.equalsIgnoreCase("PAPER")) {
System.out.println("WIN");
wins++;
} else {
System.out.println("Enter a valid choice");
}
} else {
System.out.println("You won " + wins + " matches");
System.out.println("You tied " + ties + " matches");
System.out.println("You lost " + losses + " matches");
break;
}
}
}
}
I'm getting an error in my method which says this:
Main.java:23: error: cannot find symbol
return pick;
^
symbol: variable pick
location: class Main
1 error
exit status 1
I can't figure out how to fix this error. I would appreciate your input as well as any other general advice
Thanks
Your variable is only visible in the if statement. Read about scopes.
Change to:
import java.util.Scanner;
import java.util.Random;
public class Main {
public static String comChoice() {
Random rand = new Random();
int num = rand.nextInt(299) + 1;
String pick = null;
if (num >= 1 && num <= 99) {
pick = "SCISSORS";
} else if (num >= 100 && num <= 199) {
pick = "ROCK";
} else if (num >= 200 && num <= 299) {
pick = "PAPER";
}
return pick;
}
....
}
pick is out of scope. Try declaring at the start of the comChoice method.
Hence :
public static String comChoice() {
String pick=null;
Random rand = new Random();
int num = rand.nextInt(299) + 1;
if (num >= 1 && num <= 99) {
pick = "SCISSORS";
} else if (num >= 100 && num <= 199) {
pick = "ROCK";
} else if (num >= 200 && num <= 299) {
pick = "PAPER";
}
return pick;
}
variable pick is out of scope. You have to declare it outside the all if else statements. if all if else condition fail then comChoice method will not be able to find variable pick (as it is declared inside the if else block only)which it has to return.
corrected code
import java.util.Scanner;
import java.util.Random;
public class Main
{
public static String comChoice()
{
Random rand = new Random();
int num = rand.nextInt(299) + 1;
String pick = "";
if(num >= 1 && num <= 99)
{
pick = "SCISSORS";
}
else if (num >= 100 && num <= 199)
{
pick = "ROCK";
}
else if (num >= 200 && num <= 299)
{
pick = "PAPER";
}
return pick;
}
public static void main (String args[])
{
int wins = 0;
int losses = 0;
int ties = 0;
while (1 == 1)
{
Scanner s = new Scanner (System.in);
System.out.println("Would you like to play Rock Paper Scissors? Y/N");
String n = s.next();
if (n.equalsIgnoreCase("Y"))
{
Scanner t = new Scanner (System.in);
System.out.println("Enter your pick: rock, paper, or scissors");
String userChoice = t.next();
String pick = comChoice();
if (userChoice.equalsIgnoreCase("SCISSORS") && pick.equalsIgnoreCase("SCISSORS"))
{
System.out.println("TIE");
ties++;
}
else if (userChoice.equalsIgnoreCase("ROCK") && pick.equalsIgnoreCase("SCISSORS"))
{
System.out.println("WIN");
wins++;
}
else if (userChoice.equalsIgnoreCase("PAPER") && pick.equalsIgnoreCase("SCISSORS"))
{
System.out.println("LOSE");
losses++;
}
else if (userChoice.equalsIgnoreCase("SCISSORS") && pick.equalsIgnoreCase("ROCK"))
{
System.out.println("LOSE");
losses++;
}
else if (userChoice.equalsIgnoreCase("SCISSORS") && pick.equalsIgnoreCase("PAPER"))
{
System.out.println("WIN");
wins++;
}
else
{
System.out.println("Enter a valid choice");
}
}
else
{
System.out.println("You won " + wins + " matches");
System.out.println("You tied " + ties + " matches");
System.out.println("You lost " + losses + " matches");
break;
}
}
}
}
You declared your variable pick in the if else structure. Doing this causes the problem that your variable cannot be accessed from outside that if else structure. In simple words, the variable pick's scope is limited to your if else structure. You have to declare your variable (and initialize it as well, otherwise you'll get an error in your case) outside of the if else structure. Like this:
String pick = null;
if(num >= 1 && num <= 99) {
...
...
...// All your if's and else's
}
return pick;
Hope this helps!
My code is not stopping the iteration where i use the do while loop, i wanted to only iterate when the secondThrow is not the same as the firstThrow and not a number 7????? help
import java.util.Scanner;
public class Craps_callee
{
private Die die1;
private Die die2;
public Craps_callee() //
{
die1 = new Die ();
die2 = new Die ();
}
private void rollDice()
{
die1.roll();
die2.roll();
}
private int getSum()
{
int sum = die1.getNumber()+die2.getNumber();
return sum;
}
public void playRound()
{
rollDice();
int firstThrow = getSum();
int secondThrow = getSum();
//for (int i; i )
if(firstThrow == 7 || firstThrow == 11)
{
System.out.println("You rolled a " +firstThrow+ " You Win Congratulations!");
}
else if (firstThrow == 2 || firstThrow == 3 || firstThrow == 12)
{
System.out.println("You rolled a "+firstThrow+" You Lose! ");
}
else
{
System.out.println("You rolled a: "+firstThrow);
System.out.println(" !! Establish Point !!");
System.out.println(" you need to roll another "+firstThrow+" to win before 7 is rolled");
do
{
rollDice();
secondThrow = getSum();
System.out.println("SeconndThrow is: "+secondThrow);
System.out.println("FirstThrow is: "+firstThrow);
if (secondThrow == firstThrow)
{
System.out.println("You rolled a " +secondThrow+ " Twice. You Win Congratulations!");
}
else if (secondThrow == 7)
{
System.out.println("You rolled a 7 You Lose! ");
}
}
while (secondThrow != firstThrow || secondThrow != 7);
}
}
public static void main(String[] args)
{
String prompt_from_user;
Scanner scan = new Scanner(System.in);
System.out.println("Input the amount of "+scan);
prompt_from_user = scan.nextLine();
if (prompt_from_user == "YES" || prompt_from_user == "yes");
{
}
This is the part that's wrong
do
{
rollDice();
secondThrow = getSum();
System.out.println("SeconndThrow is: "+secondThrow);
System.out.println("FirstThrow is: "+firstThrow);
if (secondThrow == firstThrow)
{
System.out.println("You rolled a " +secondThrow+ " Twice. You Win Congratulations!");
}
else if (secondThrow == 7)
{
System.out.println("You rolled a 7 You Lose! ");
}
}
while (secondThrow != firstThrow || secondThrow != 7);
I wanted to only iterate when the > secondThrow is not the same as > > the firstThrow and not a number 7
Read the sentence and realise then that your condition should implement an AND instead of an OR
So I am lost. This is the goal:
Ask how many times to roll one six-sided dice.
Randomly roll this dice.
Print out how many times each number between one and six appeared
Is this loop thing on the right track? What should I begin with I'm really confused sorry
while(dice > 0)
{
rolldice = gen.nextInt(6) + 1; //(1-6)
if (rolldice == 1)
{
one++;
}
else if (rolldice == 2)
{
two++;
}
else if (rolldice == 3)
{
three++;
}
else if (rolldice == 4)
{
four++;
}
else if (rolldice == 5)
{
five++;
}
else if (rolldice == 6)
{
six++;
}
}
Scanner in = new Scanner(System.in);
Random gen = new Random();
int rollDice;
int one = 0, two = 0, three = 0, four = 0, five = 0, six = 0;
System.out.println("How many die do you want to roll: ");
int dice = in.nextInt();
while(dice > 0)
{
rolldice = gen.nextInt(6) + 1; //(1-6)
if (rolldice == 1)
{
one++;
}
else if (rolldice == 2)
{
two++;
}
else if (rolldice == 3)
{
three++;
}
else if (rolldice == 4)
{
four++;
}
else if (rolldice == 5)
{
five++;
}
else if (rolldice == 6)
{
six++;
}
dice--;
}
Now just print out the variables and you are done!
I could also do this with a switch statement, but I'll let you figure out how to convert if thats what you want to use.
Im assuming the dice integer is the input from the user. If you decrement dice each time you iterate, you will roll the dice(go through the loop) as many times as the user asked.
Not sure if you have implemented it so that the user can input a number of dicerolls yet, if thats what you need help with take a look at Scanner.
I'm currently on a self learning course and need to write a Test program for the following code and repeat steps by assigning different values to the member field.
public class DayTwo {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public void displayDay() {
int dayNumber = 1;
if (dayNumber == 1) {
System.out.println("Monday");
} else if (dayNumber == 2) {
System.out.println("Tuesday");
} else if (dayNumber == 3) {
System.out.println("Wednesday");
} else if (dayNumber == 4) {
System.out.println("Thursday");
} else if (dayNumber == 5) {
System.out.println("Friday");
} else if (dayNumber == 6) {
System.out.println("Saturday");
} else if (dayNumber == 7) {
System.out.println("Sunday");
} else {
System.out.println(dayNumber + " entered. This is not a valid date.");
}
}
}
Can anybody help?
If you want to purely test the code you have, remove public static void main(String[] args){} from DayTwo as it's not needed, and use the following Test class:
public class TestDay2 {
public static void main(String[] args) {
DayTwo example = new DayTwo();
example.displayDay();
}
}
However this will always print 'Monday' due to dayNumber being hard coded. You'll have to change this to test each if statement as I assume you haven't touched on loops yet.
int dayNumber = 1; it is hard coded value and will always satisfy the first condition skipping the rest of the statements, instead take input using Scanner or BufferedInputStream
Scanner sc = new Scanner(System.in);
int dayNumber = sc.nextInt();
First o all, Classes that dont run by themselves dont come with a main method. Another mistake of your code is that it will always print "Monday".
For making a test class
public class Test{
public static void main(String[] args){
DayTwo dayTwo = new DayTwo();
dayTwo. displayDay();
}
}
Something like this....
import java.util.*;
public class DayTwo
{
public static void main(String[] args)
{
int number,choice=1;
Scanner sc = new Scanner(System.in);
while(choice!=0)
{
System.out.println("Enter the day number: ");
number=sc.nextInt();
displayDay(number);
System.out.println("Press 0 to exit, 1 to continue:");
choice=sc.nextInt();
}
}
public static void displayDay(int dayNumber)
{
if (dayNumber == 1) {
System.out.println("Monday");
} else if (dayNumber == 2) {
System.out.println("Tuesday");
} else if (dayNumber == 3) {
System.out.println("Wednesday");
} else if (dayNumber == 4) {
System.out.println("Thursday");
} else if (dayNumber == 5) {
System.out.println("Friday");
} else if (dayNumber == 6) {
System.out.println("Saturday");
} else if (dayNumber == 7) {
System.out.println("Sunday");
} else {
System.out.println(dayNumber + " entered. This is not a valid date.");
}
}
}
To follow the instructions just cut and paste the dayNumber declaration so it is a member field (between class declaration and main declaration). Then create a new DayTwo in main and you can assign dayNumber to different things and print it.
public class DayTwo {
// now a dayNumber belongs to every DayTwo object
int dayNumber = 1;
// main is static and belongs to the DayTwo class
public static void main(String[] args) {
// make a new DayTwo object
DayTwo days = new DayTwo();
// displays Monday
days.displayDay();
days.dayNumber = 4;
// displays Thursday
days.displayDay();
for (int i = 1; i <= 7; i++) {
days.dayNumber = i;
// displays all days
days.displayDay();
}
}
public void displayDay() {
if (dayNumber == 1) {
System.out.println("Monday");
} else if (dayNumber == 2) {
System.out.println("Tuesday");
} else if (dayNumber == 3) {
System.out.println("Wednesday");
} else if (dayNumber == 4) {
System.out.println("Thursday");
} else if (dayNumber == 5) {
System.out.println("Friday");
} else if (dayNumber == 6) {
System.out.println("Saturday");
} else if (dayNumber == 7) {
System.out.println("Sunday");
} else {
System.out.println(dayNumber + " entered. This is not a valid date.");
}
}
}
You don't need anything more complicated than that.
Here is my coding:
For some reason, at the very end, when I'm trying to return winscounter and losscounter, it says unreachable statement but not for the tiescounter. I can't figure out why! If anyone can answer this, it would be greatly appreciated!!
public class RockPaperScissors {
/**
* #param args the command line arguments
*/
static int value; //computer's choice
static int choice; //user choice
static int tiescounter = 0;
static int winscounter = 0;
static int losscounter = 0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));// user input
int repeat;
do {
System.out.println("ROCK PAPER SCISSORS"+
"\n===================");
System.out.println("\n1=Rock" +
"\n2=Paper" +
"\n3=Scissors" +
"\n===========" +
"\nChoose:");
choice = Integer.parseInt(br.readLine());
if (choice !=1 && choice !=2 && choice !=3) {
do{
System.out.println("\nError. Please choose Rock, Paper or Scissors.");
choice = Integer.parseInt(br.readLine());
}
while (choice !=1 && choice !=2 && choice !=3);
}
System.out.println();
if (choice == 1){
System.out.println("You have chosen Rock.");
}
else if (choice ==2){
System.out.println("You have chosen Paper.");
}
else if(choice == 3){
System.out.println("You have chosen Scissors.");
}
randomWholeNumber();
if (value == 1){
System.out.println("The computer has chosen Rock." );
}
else if (value == 2){
System.out.println("The computer has chosen Paper." );
}
else if (value == 3){
System.out.println("The computer has chosen Scissors." );
}
determineOutcome();
System.out.println("Ties:"+ tiescounter);
System.out.println("Wins: " + winscounter);
System.out.println("Losses: " + losscounter);
repeat = Integer.parseInt(br.readLine());
}
while (repeat==1);
}
public static int randomWholeNumber(){
do{
value=0;//resets random number
//generates and returns a random number within user's range
value = (int) ((Math.random()*3)+1);
}
while((value>3)||(value<1));
return (value);
}
public static int determineOutcome(){
if (value == choice){
System.out.println("\nYOU'VE TIED");
do{
tiescounter+=1;
}
while (tiescounter != tiescounter);
}
else if (value == 1){ //Rock
if (choice == 2){ //Paper
System.out.println("\nYOU'VE WON");
do{
winscounter +=1;
}
while (winscounter != winscounter);
}
else if (choice == 3){ //Scissors
System.out.println("\nYOU'VE LOST");
do{
losscounter+=1;
}
while(losscounter!=losscounter);
}
}
else if (value == 2){ //Paper
if (choice == 1){ //Rock
System.out.println("\nYOU'VE LOST");
do{
losscounter+=1;
}
while(losscounter!=losscounter);
}
else if (choice == 3){ //Scissors
System.out.println("\nYOU'VE WON");
do{
winscounter +=1;
}
while (winscounter != winscounter);
}
}
else if (value == 3){ //Scissors
if (choice == 1){ //Rock
System.out.println("\nYOU'VE WON");
do{
winscounter +=1;
}
while (winscounter != winscounter);
}
else if (choice == 2){ //Paper
System.out.println("\nYOU'VE LOST");
do{
losscounter+=1;
}
while(losscounter!=losscounter);
}
}
return(tiescounter);
return(winscounter);
return(losscounter);
}
}
You cannot return 3 different variables from one method, all at the same time. In this case, the return(tiescounter); always executes, terminating the method then and there. Hence the next 2 lines become unreachable.
Declare determineOutcome() method as void i.e. public static void determineOutcome(), and remove all return statements inside it. Your program will work.
A function can only return one value. As soon as return(tiescounter); is executed the function exits. If you want to return all three values you will have to wrap them in a class.
By the way return(tiescounter); can be written as return tiescounter; Parenthesis around return values is not required. Both statements will have the same result.