I'm trying to get my loop to go over and over again until the user types in "quit" then it will quit.
Basically I'm trying to get my Finch robot to change nose color depending on how it's positioned, but I'm confused on how to get it to allow the user to position it again after it has already been positioned so that the nose color can change color multiple times. When it first runs the Finch will execute the code but quits immediately afterwards.
Here is my code:
public class finch {
public static final int INCREASE = 20;
public static final int SEC = 1000;
public static final int MAXSPEED = 255;
public static final int HALFSEC = 500;
public static Finch myFinch;
public static void main(String[] args) {
myFinch = new Finch();
Menu();
}
public static void Menu() {
Scanner console = new Scanner(System.in);
System.out.println("Enter your choice:" + "");
int input;
int input1;
boolean flag=true;
while(flag){
System.out.println("1.\t" + "Rolling" + "Finch");
System.out.println("2.\t" + "Obedient" + "Finch");
System.out.println("3.\t" + "Exit");
System.out.println();
System.out.println("Enter your choice:");
System.out.println();
input = console.nextInt();
flag=false;
if (input == 1) {
//input = DarkFinch;
System.out.println("Position the Finch \"down\" or \"up\" to change nose color");
rolling(myFinch);
} else if (input == 2) {
// input = ChasetheFinch;
// System.out.println("Chase The Finch");
} else if (input == 3) {
// input = Exit;
System.out.println("Goodbye");
} else {
// System.out.println("Try again");
flag=true;
/* return Menu(); */
}
}
}
public static boolean rolling(Finch myFinch) {//This method will change the Finches nose color depending on the Finches position.
//"up" = place the finch upright standing on its tail
for (int i = 1; i <= 20; i++) {
while (myFinch.isBeakDown() || myFinch.isBeakUp()) {
if (myFinch.isBeakDown()) {
myFinch.setLED(0,0,255,3000);
} else if (myFinch.isBeakUp()) {
myFinch.setLED(255,0,0,3000);
} else {
myFinch.setLED(0,0,0,5000);
}
}
}
return true;
}
}
Don't set your flag = false before the condition on the input value. Set it to false in the if(input == 3) case
Related
Im writing a program similar to a game called shut the box. The game asks the player to roll 2 die, and then the player chooses to cover both numbers individually (in the boolean array), or the total of the two.
I'm having difficulty returning two values of the int method roll() which is supposed to roll two die.
Heres my code for the main class:
public class clacker {
private int play;
private int die1;
private int die2;
private boolean[] table;
private final int high = 6;
private final int low = 1;
private int range;
public clacker()
{
range = high-low+1;
table = new boolean[13];
play = 0;
}
public void roll()
{
die1 = (int)(Math.random()*range+low);
die2 = (int)(Math.random()*range+low);
}
public void value(char ch)
{
if(ch == 'i' || ch == 'I')
{
table[die1] = true;
table[die2] = true;
displayBoard();
}
else if(ch == 'T' || ch == 't')
{
table[die1+die2] = true;
displayBoard();
}
else
{
roll();
}
}
public void displayBoard()
{
for(int i = 1; i<table.length; i++)
{
if(table[i] == false)
{
System.out.print(" " + i + " ");
}
else
{
System.out.print(" / ");
}
}
}
}
and for the test class:
public class clackerTest {
public static void main(String[] args) {
clacker oo = new clacker();
EasyReader kboard = new EasyReader();
System.out.println("Press anything to roll the dice. ");
char roll= kboard.readChar();
int dice1 = oo.roll();
int dice2 = oo.roll();
System.out.println("You rolled " + dice1 + " and a " + dice2 + "!");
System.out.println("Cover Individual or Total: (enter i or t)");
char roll2 = kboard.readChar();
}
}
Why don't you just roll() twice? Modify roll() to return one int.
public int roll() {
return (int)(Math.random()*range+low);
}
And now, from within clackerTest (java conventions dictates you name this ClackerTest):
int dice1 = oo.roll();
int dice2 = oo.roll();
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I haven't asked many questions on here so sorry if I have laid this question out wrongly.
I am trying to call the variable AmountOfTickets in the main class. So I can update it with each loop of the while loop. I don't know how to properly call the variable without eclipse showing an error.
This is the class where I have created the variable AmountOfTickets which
I am trying to update
public class TicketHandler {
public int TicketCost;
public static int AmountOfTickets;
public TicketHandler(int TicketCost, int AmountOfTickets){
this.TicketCost = TicketCost;
this.AmountOfTickets = AmountOfTickets;
}
public TicketHandler() {
TicketCost = 200;
AmountOfTickets = 50;
}
public int getTicketCost() {
return TicketCost;
}
public void setTicketCost(int ticketCost) {
TicketCost = ticketCost;
}
public static int getAmountOfTickets() {
return AmountOfTickets;
}
public void setAmountOfTickets(int amountOfTickets) {
AmountOfTickets = amountOfTickets;
}
public void ReduceNoOfTickets(int TicketsSold) {
this.AmountOfTickets = this.AmountOfTickets - TicketsSold;
}
}
This is the main program code where I am trying to call the variable and used it
import java.util.Scanner;
public class ApplicationProgram {
static Scanner inKey = new Scanner(System.in);
public static void main(String[] args) {
TicketHandler Red = new TicketHandler (200,50);
TicketHandler Green = new TicketHandler (200,50);
TicketHandler Blue = new TicketHandler (200,50);
TicketHandler Yellow = new TicketHandler (200,50);
boolean running = true;
//Main menu
while (running) {
printMenu();
int KeyIn = inKey.nextInt();
//menu
switch (KeyIn) {
case 1:{
RedRoute();
break;
}
case 2:{
GreenRoute();
break;
}
case 3:{
BlueRoute();
break;
}
case 4:{
YellowRoute();
break;
}
case 5:{
ShowAvailableTicketsAndPrice();
break;
}
case 6:{
System.out.println("Do you wish to quit: type Y to confirm");
String quitYN = inKey.next ();
//System.out.println(quitYN);
if ((quitYN.matches("Y")) || (quitYN.matches("y"))) {
System.out.println("Goodbye\n\n");
running = false;
System.exit(0);
}
break;
}
default:{
System.out.println("Not a valid option, try again.");
break;
}
}
}
}
public static void printMenu(){
System.out.println("Bus Tickets");
System.out.println("---- ----");
System.out.println("1. \tRedRoute");
System.out.println("2. \tGreenRoute");
System.out.println("3. \tBlueRoute");
System.out.println("4. \tYellowRoute");
System.out.println("5. \tShowAvailableTicketsAndPrice");
System.out.println("6. \tQuit");
}
public static void RedRoute(){
Scanner Red = new Scanner(System.in);
System.out.println();
int count = 200;
while (count <= 200) {
System.out.println("Insert £" + count);
int KeyIn = Red.nextInt();
count = count - KeyIn;
if (count == 0){
System.out.println("Enjoy");
break;
}
}
}
public static void GreenRoute(){
Scanner Red = new Scanner(System.in);
System.out.println();
int count = 200;
while (count <= 200){
System.out.println("Insert £" + count);
int KeyIn = Red.nextInt();
count = count - KeyIn;
if (count == 0){
System.out.println("Enjoy");
break;
}
}
}
public static void BlueRoute(){
Scanner Red = new Scanner(System.in);
System.out.println();
int moneyin = 200;
while (moneyin <= 200){
System.out.println("Insert £" + moneyin);
int KeyIn = Red.nextInt();
moneyin = moneyin - KeyIn;
if (moneyin == 0){
System.out.println("Enjoy");
AmountOfTickets = get.AmountOfTickets - 1
break;
}
}
}
public static void YellowRoute(){
Scanner Red = new Scanner(System.in);
System.out.println();
int count = 200;
while (count <= 200) {
System.out.println("Insert £" + count);
int KeyIn = Red.nextInt();
count = count - KeyIn;
if (count == 0){
System.out.println("Enjoy");
break;
}
}
}
public static void ShowAvailableTicketsAndPrice(){
System.out.println("Routes available:");
System.out.println("\tRed route, Tickets left: "+ "Price: ");
System.out.println("\tBlue route, Tickets left: " + "Price: ");
System.out.println("\tGreen route, Tickets left: " + "Price: ");
System.out.println("\tYellow route, Tickets left: " + "Price: ");
}
}
Thank you.
First of all, your var called AmountOfTickets is static, it means one copy will live to all objects, also your method getAmountOfTickets.
you have a problem here
AmountOfTickets = get.AmountOfTickets - 1
you must call the variable using the ClassName.YourVar like this:
TicketHandler.AmountOfTickets = TicketHandler.getAmountOfTickets() - 1;
also your method.
Also you could import a static like this:
import java.util.Scanner;
import static huh.TicketHandler.*;
so you can use:
AmountOfTickets = getAmountOfTickets() - 1;
"You could think in create an object and call the var like this"
public static void BlueRoute(){
Scanner Red = new Scanner(System.in);
TicketHandler objectOne = new TicketHandler(); //HERE I CREATED AN OBJECT
System.out.println();
int moneyin = 200;
while (moneyin <= 200)
{
System.out.println("Insert £" + moneyin);
int KeyIn = Red.nextInt();
moneyin = moneyin - KeyIn;
if (moneyin == 0){
System.out.println("Enjoy");
objectOne.AmountOfTickets = objectOne.getAmountOfTickets() - 1; //HERE I USED IT
break;
}
But as this is static it will replace objectOne to
TicketHandler.AmountOfTickets = TicketHandler.getAmountOfTickets() - 1; //HERE I USED IT
So, in my humble opinion you can or use an import static (so you avoid to write the same class name before your static), or put the name of your class first then the name and method.
Using an import static could be bad if there is another class with the same name or method as yours and you imported, because compiler could say, hey which one am I suppose to use?
So, I'd rather use fullnameClass.varOrMethod
hope it helps
btw using this: TicketHandler.AmountOfTickets and this: TicketHandler.getAmountOfTickets() are avable to be used in all ApplicationProgra's class code as i said because it is static, and because you difined them public
public static int AmountOfTickets;
public static int getAmountOfTickets() {
return AmountOfTickets;
}
=) i guess that was what you asked for. c-ya
public static void main(String[] args) {
Scanner inScanner = new Scanner(System.in);
int[] dice = new int[5];
resetDice(dice);
System.out.print("Your current dice: + dice");
rollDice(dice);
System.out.print(dice);
System.out.println();
promptForReroll(dice, inScanner);
System.out.println("Rerolling...");
rollDice(dice);
System.out.println("Your final dice: + dice");
System.out.println(getResult(dice)+"!");
}
private static void resetDice(int[] dice) {
int length = 5;
for(int i = 0; i < length; i++){
dice[i] = 0;
}
}
private static void rollDice(int[] dice) {
int i = 0;
int length = 5;
while(i < length) {
if(dice[i] == 0) {
int roll = (int)(Math.random()*6)+1;
dice[i] = roll;
i++;
}
}
}
private static String diceToString(int[] dice) {
String diceToString =("Your current dice: " +dice[0]+", " +dice[1]+ ", " +dice[2]+ ", " +dice[3]+ ", " +dice[4]);
return diceToString;
}
private static void promptForReroll(int[] dice, Scanner inScanner) {
System.out.print("Select a die to re-roll (-1 to keep remaining dice): ");
int selection = inScanner.nextInt();
while(selection != -1){
if(selection > 4 || selection < -1){
System.out.println("Erorr: Index must be between 0 and 4 (-1 to quit)!");
}
else{
dice[selection] = 0;
}
System.out.println("Your current dice: " + dice);
System.out.print("Select a die to re-roll (-1 to keep remaining dice): ");
selection = inScanner.nextInt();
}
System.out.println("Keeping remaining die...");
}
private static boolean promptForPlayAgain(Scanner inScanner) {
System.out.println("Would you like to play again?(Y or N)");
String play = inScanner.nextLine();
if(play=="Y"){
return true;
}
else if(play=="N"){
return false;
}
}
private static String getResult(int[] dice) {
getCounts(dice);
}
private static int[] getCounts(int[] dice) {
int[] count = new int[6];
int i = 0;
int j = 0;
while(i<5){
while(j<5){
if(dice[i] == (j+1)){
count[j]++;
i++;
}
else{
i++;
}
j++;
}
}
return count;
}
I have written out the coding, and I think for the most part it may be right, but I can't run it because it has errors on my promptForPlayAgain method, and my getResult method. I don't know why there are errors on them.
You aren't returning anything if the user's input isn't "Y" or "N". You need to add an else case:
private static Boolean promptForPlayAgain(Scanner inScanner) {
System.out.println("Would you like to play again?(Y or N)");
String play = inScanner.nextLine();
if(play=="Y"){
return true;
}
else if(play=="N"){
return false;
}
else
{
//Do something here to handle bad input.
}
}
Ideally though, you should be doing some kind of formatting and preverification of the input, like forcing the input to upper case before comparing.
And on getResult, you aren't returning anything. Add a return before the body of the function. That won't fix it though, since the return type of getValue doesn't match getResult. You're trying to return a int array as a string.
In the future, please include the actual error messages they you receive.
public class game {
int[] locks;
int numberofhits = 0;
boolean isAlive = true;
int numberofguess = 0;
public void setlocation(int[] location) {
locks = location;
}
public String checkyourself(String guess) {
int guess1 = Integer.parseInt(guess);
String result = "missed";
System.out.println("Guess by Player =" + guess);
for (int cell : locks) {
if (guess1 == cell) {
result = "killed";
isAlive = false;
numberofhits++;
break;
}//if close
}//for close
System.out.println("Player you have " + result + " the object");
while (isAlive == true) {
System.out.println("Enter another guess buddy");
break;
}
while (isAlive == false) {
System.out.println("You hit the object");
break;
}
System.out.println("Number of object hit by the player is " + numberofhits);
return result;
}//method close
}//class close
public class rungame {
public static void main(String[] args) {
int randomnumber = (int) (Math.random() * 8);
int[] location = {randomnumber, randomnumber + 1,
randomnumber + 2, randomnumber + 3};
game player = new game();
player.setlocation(location);
player.checkyourself("0");
System.out.println("Game is Started ...........");
player.checkyourself("1");
player.checkyourself("2");
player.checkyourself("3");
player.checkyourself("4");
player.checkyourself("5");
player.checkyourself("6");
player.checkyourself("7");
}//Main Close
}//Class close
I want to count the guess made by the user. It is possible to do in this code?
And how can i improve this code? The code is running perfectly right, but i want to count the user guess and is it possible to write while(isAlive==true) condition in main method?
Is it possible to limit the user guess with the length of location array?
Well if i understood you corectly adding
numberofguess++;
in checkyourselft method will solve your problem.
This question already has answers here:
Syntax Errors in QuizGame and Tips [closed]
(2 answers)
Closed 9 years ago.
I am trying to make a quiz game. I was wondering when I run the program, I get an infinite loop on my while statements. When I run my code after I entered the game it prints the question, but before I input my answer it gives me an Exception error. I am not quite sure on how to fix it.
/**
* #(#)QuizGameFinal2.java
*
*
* #author
* #version 1.00 2013/4/30
*/
import java.util.Scanner;
import java.lang.Math;
import java.lang.String;
public class QuizGameFinal2
{
public static void main(String[]args)
{
int option_Selected = 0;
int option_Single_Player = 1;
int option_Multiplayer = 2;
int answer = 0;
int player_One_Answer = 0;
int player_Two_Answer = 0;
String response;
int player_One_Winnings = 0;
int player_Two_Winnings = 0;
int winnings = 0;
int computer_Winnings = 0;
double computer_Answer = 0;
Scanner input2 = new Scanner(System.in);
say_Intro();
say_Before_First_Question();
question_One();
human_Answer();
while (answer < 1 && answer >= 4);
{
System.out.println("Enter a number between 1 and 3");
}
if (answer == 1)
{
System.out.println("Correct Next Question");
question_Two();
human_Answer();
while (answer < 1 && answer >= 4);
{
System.out.println("Enter a number between 1 and 3");
}
if (answer == 1 )
{
System.out.println("Correct Next Question");
question_Three();
human_Answer();
while (answer < 1 && answer > 4);
{
System.out.println("Enter a number between 1 and 3");
}
if (answer == 4)
{
System.out.println("Correct Next Question");
}
else
{
System.out.println("Incorrect you win 1000 dollers");
winnings = 1000;
}
}
else
{
System.out.println("Incorrect you win 500 dollers");
winnings = 500;
}
}
else
{
System.out.println("Incorrect you win 0 dollers");
winnings = 0;
}
} // End of main method
public static void say_Intro() // Intro Method
{
System.out.println("Welcome to the QuizGame"); // Player selects which mode
}
public static void say_Before_First_Question() // Before game method
{
System.out.println("");
System.out.println(" If you get a question wrong your out.");
System.out.println("Also you will be competing against a super computer, after you play then he will generate answers, if you have the most then you win");
System.out.println("Ok first question");
}
public static void human_Answer () // Human Answer Single Player Method
{
Scanner input2 = null;
Object answer = input2.nextInt();
}
static void player_One_Answer() // Player One Multiplayer Method
{
Scanner input2 = null;
int player_One_Answer = input2.nextInt();
}
public static void player_Two_Answer() // Player two multiplayer Method
{
Scanner input2 = null;
int player_Two_Answer = input2.nextInt();
}
public static void computer_Answer () // Computer answer
{
double computer_Answer = (1-1 + 1) * Math.random() + 1;
computer_Answer = (int)computer_Answer;
}
public static void question_One() // Question 1 method
{
System.out.println("");
System.out.println("What is an application");
System.out.println("1: A program that performs a task 2:A mouse 3: java.util.Scanner");
}
public static void question_Two() // Question 2 to 10 methods below
{
System.out.println("What is the data type that hold the value 1");
System.out.println("1: int 2:float 3: short 4: long ");
}
public static void question_Three()
{
System.out.println("What is a not a high level language");
System.out.println("1: Java 2:C++ 3: Colbolt 4: Machine Language ");
}
/* public static void question_Five()
{
}
public static void question_Six()
{
}
public static void question_Seven()
{
}
public static void question_Eight()
{
}
public static void question_Nine()
{
}
public static void question_Ten()
{
}
public static void total_Winnings_Single_Player () // Calculating who wins method single player
{
if (computer_Winnings > winnings)
{
System.out.println("Computer Wins");
}
else
{
System.out.println("You win");
}
*/
}
// I get infinite loop om my while statements and I get an exepction error on my values with null
This is because input2 is null try this:
Scanner input2 = new Scanner(System.in);