My point tracker is reseting the points for both players to the original number every time I do it more than once.
I can't figure out why whenever I take away more life-points from either player, instead of using the previous number of life-points, it just resets to whatever I made the life-points start out on and goes form there.
EX:
player-1 has 100 life points.
I take away 1 life-point.
player-1 now has 99 life-points.
Now I do it again.
I take away 1 more life-point from player-1.
But now, instead of taking 1 away from the previous life-point count of 99, it takes 1 away from 100 again and gives me 99 a second time.
I can't tell where I made a mistake that keeps reseting the scores.
package yu.gi.oh.life.point.counter;
import java.util.Scanner;
public class YuGiOhLifePointCounter {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
double choice_1;
System.out.print("\nEnter the starting life points for player-1: ");
choice_1 = sc.nextDouble();
double storage_1;
storage_1 = choice_1;
double choice_2;
System.out.print("\nEnter the starting life points for player-2: ");
choice_2 = sc.nextDouble();
double storage_2;
storage_2 = choice_2;
double lp1;
System.out.print("\nEnter a 6 to change the life-points of either player: ");
lp1 = sc.nextDouble();
while (lp1 == 6) {
double choose_1_2;
System.out.print("\nEnter a 1 to change player-1's life-points, or enter a 2 to change player-2's life-points: ");
choose_1_2 = sc.nextDouble();
if (choose_1_2 == 1) {
double ch_1;
System.out.print("\nEnter the number subtracted from or added to player-1's life-points: ");
ch_1 = sc.nextDouble();
double c_1;
System.out.print("\nEnter a 1 to subtract this number from player-1's life-points, or enter a 2 to add this number to player-1's life-points: ");
c_1 = sc.nextDouble();
double display_1;
if (c_1 == 1) {
display_1 = storage_1 - ch_1;
System.out.println("\nPlayer-1's life-points are currently " + display_1);
}
if (c_1 == 2) {
display_1 = storage_1 + ch_1;
System.out.println("\nPlayer-1's life-points are currently " + display_1);
}
}
if (choose_1_2 == 2) {
double ch_2;
System.out.print("\nEnter the number subtracted from or added to player-2's life-points: ");
ch_2 = sc.nextDouble();
double c_2;
System.out.print("\nEnter a 1 to subtract this number from player-2's life-points, or enter a 2 to add this number to player-1's life-points: ");
c_2 = sc.nextDouble();
double display_2;
if (c_2 == 1) {
display_2 = storage_2 - ch_2;
System.out.println("\nPlayer-2's life-points are currently " + display_2);
}
if (c_2 == 2) {
display_2 = storage_2 + ch_2;
System.out.println("\nPlayer-2's life-points are currently " + display_2);
}
}
lp1 = 6;
}
}
}
You never change the storage_1/2 values.
A line like display_2 = storage_2 - ch_2; will keep calculating the difference with the original number of life points (100), instead of subtracting from the previously calculated amount. Try updating these storage_x values after you calculate the display_x values.
Related
first question here.
So for both of these loops I am looking for the user to input a value less than 10 for loop 1 and less than 200 for loop 2. It is almost working to my liking however when a user enters an incorrect number the loop just exits where it should repeat and ask the user for another digit smaller than 10/200.
Any assistance is greatly appreciated.
public class Main {
public static int numberOfStars;
public static void main(String[ ] args){
//ask for number of stars (user-input)
System.out.println("Enter the number of stars in your constellation");
Scanner stars = new Scanner(System.in);
if (numberOfStars <= 10) {
numberOfStars = stars.nextInt();
}do{
System.out.println("The number of stars is : " + numberOfStars);
} while (numberOfStars <= 10);
//ask for location of stars (user-input)
System.out.println("Enter X and Y co-ordinates for your constellation");
//obj 1
Scanner myObj = new Scanner(System.in);
while(myObj.nextInt() <= 200) {
int location = myObj.nextInt();
System.out.println("X coordinate 1 is : " + location);
} do {
System.out.println("Please enter a Number Less than 200");
} while (myObj.nextInt() > 200 );
You could put all the code in your main mathod in a while(true) loop as you are invoking a blocking method. If you succeed (if the value is correct) you can just break the main loop (e.g. by marking it with a label). Otherwise continue the main loop which makes the input prompt appear again.
I am trying to find means to compute future weekdays, given two inputs:
Current weekday (range from 0-6 where 0 is Sunday).
How many counts to perform from current weekday (any number)?
Here if the user previously starts from current weekday = 3, and
counts=7.
Then I expect it to come back to 3, similarly with 14 or 21.
How to generalize this to make the counts within this fixed range
0-6 without pulling out of it?
I've already done some code which is posted below,
public class ThoseDays {
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
System.out.print("Enter number between 0-6 : ");
int startFromHere = obj.nextInt();
System.out.print("Enter number to count position from " + startFromHere + " : ");
int rotateFromHere = obj.nextInt();
System.out.print( startFromHere + rotateFromHere);
obj.close();
}
}
Actual result:
> Enter the number between 0-6: 3
> Enter the number to count position from 3: 7
> 10
Expected result:
> Enter the number between 0-6: 3
> Enter the number to count position from 3: 7
> 3
Hi i suggest you just use a modulo to rotate the days after they reach 7. Another tutorial here
public class ThoseDays {
public static void main(String[] args) {
//Scanner implements AutoCloseable
//https://docs.oracle.com/javase/8/docs/api/java/lang/AutoCloseable.html
try (Scanner obj = new Scanner(System.in)) {
System.out.print("Enter number between 0-6 : ");
int startFromHere = obj.nextInt();
System.out.print("Enter number to count position from " + startFromHere + " : ");
int rotateFromHere = obj.nextInt();
int absoluteNumber = startFromHere + rotateFromHere;
System.out.println(absoluteNumber);
int rotatedNumber = absoluteNumber % 7;
System.out.println(rotatedNumber);
}
}
}
code:
import java.util.*;
public class ThoseDays {
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
System.out.println("note : 0:sun 1-6:mon-saturday");
System.out.println("Enter the number between 0-6: ");// 0:sun 1-6:mon-saturday
int startFromHere = obj.nextInt();
System.out.println("Enter the number to count position from " + startFromHere+ ": ");
int rotateFromHere =obj.nextInt();
if(rotateFromHere%7==0)
{
System.out.println(startFromHere);
}
if(rotateFromHere%7!=0)
{
int dayOfWeek=(startFromHere+(rotateFromHere%7));
if(dayOfWeek>=7)
{
System.out.println((dayOfWeek%7));
}
else
{
System.out.print(dayOfWeek);
}
}
obj.close();
}
}
try this code by changing conditions and using modulo I'm getting all correct results
output:
startFromHere = 3
rotate fromHere = 7 or 14 or 21 or multiple of 7
gives the same date as the start date
if rotate date is > start date
for ex:
startFromHere = 3 //wednesday
rotateFromHere = 11
output will be : 0 which means sunday
check this code and give me a rating if useful thanks.
import java.util.*;
class Sept1Little {
public static void main (String args []) {
Scanner s = new Scanner(System.in);
System.out.println("Hey! I'm not gonna program today o.o but I'm gonna do a little program :3");
System.out.println("Let's play with some math!");
System.out.println("Enter two numbers and magic will happen");
System.out.print("Enter the first number: ");
float a = s.nextInt();
System.out.print("Enter the second number: ");
float b = s.nextFloat();
float c = (float) Math.pow( a, b);
System.out.printf("The number is :%f\n" , c);
System.out.println("We love Bernie Sanders!");
System.out.println("Come on, let's push him over the line!");
int poll1 = 30;
System.out.println("Current poll amount: " + poll1);
System.out.println("Let's add 7% and an extra 1%!");
int poll2 = poll1 + 7;
int poll3 = ++poll2;
System.out.println("Bernie Sanders would get :" + poll3 + ". Bernie would get a better percentage than Hillo Clinto-Money");
System.out.println("Write in the best US President Nominee: ");
String president = Keyboard.readString();
if (president.equals("Bernie Sanders")){
System.out.println("You chose Bernie Sanders.");
}
else if (president.equals("Donald Trump")) {
System.out.println("You chose Donald Trump");
}
else if (president.equals("Hillary Clinton")) {
System.out.println("You chose Hillary Clinton ");
}
else {
System.out.println("Choose someone we know...");
}
System.out.println("Let's calculate how much money you owe the govt by... SQUARE ROOTING IT!");
System.out.print("Enter the amount of money you owe the govt: ");
float debt1 = Keyboard.readFloat();
System.out.printf("The amount is now :%f\n" , Math.sqrt(debt1));
System.out.println("Welcome to the Euro to Dollar Convertor!");
System.out.print("Enter the amount in US Dollars you wish to convert to Euro: ");
float euro1 = Keyboard.readFloat();
final float rate = (float) 1.13;
System.out.printf("The amount in Euro is %f\n" , euro1 / rate);
}
}
As you can see, I am using Keyboard.readString(); when I ask for the nominee. When I use s.readLine();, it doesn't work, it skips to the else clause and then it crashes since it takes the president input for the float value afterwards. I'm sorry that it is a bit political but I didn't bother fiddling with it. When I put "Bernie Sanders" as "BernieSanders", I don't get the error, anyone knows why?
I guess that it is because s.nextFloat(); and s.nextInt(); only reads the float and the int, respectively, not the full line so the cursor of the Scanner it's after the float or the int and not in the next line. Here an example:
If your input it's:
5 Bench
Prove
And you have in your code:
a= s.nextInt(); //5
b= s.nextLine(); //" Bench"
Why it isn't catching the Prove value? Because after using s.nextInt() the cursor of the Scanner it's after the 5 and not in the next line (the line in which we have Prove). To avoid it you have to read the rest of the line (useless for you because you don't want it) but without getting this value. Like this:
a= s.nextInt(); //5
s.nextLine(); //We go to the next line
b= s.nextLine(); //"Prove"
P.S.: You have to use s.nextLine(); each time you use a s.nextInt(); or s.nextFloat(); as the example that I put above.
I expect it will be helpful for you!
Very simple program calculating travel distance(just started a week ago) and I have this loop working for a true or false question, but I want it to work for a simple "yes" or "no" instead. The String I have assigned for this is answer.
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double distance;
double speed;
boolean again = true;
String answer;
do{
System.out.print("Tell me your distance in miles: ");
distance = input.nextDouble();
System.out.print("Tell me your speed in which you will travel: ");
speed = input.nextDouble();
double average = distance / speed;
System.out.print("Your estimated time to your destination will be: " + average + " hours\n\n");
if(average < 1){
average = average * 10;
System.out.println(" or " + average + " hours\n\n");
}
System.out.println("Another? ");
again = input.nextBoolean();
}while(again);
}
}
You need to use input.next() instead of input.nextBoolean(), and compare the result to a string literal "yes" (presumably, in a case-insensitive way). Note that the declaration of again needs to change from boolean to String.
String again = null;
do {
... // Your loop
again = input.nextLine(); // This consumes the \n at the end
} while ("yes".equalsIgnoreCase(again));
just do
answer = input.nextLine();
} while(answer.equals("yes"));
You might want to consider being more flexible though. For example:
while (answer.startsWith("Y") || answer.startsWith("y"));
String answer=null;
do{
//code here
System.out.print("Answer? (yes/no) :: ");
answer=input.next();
}while(answer.equalsIgnoreCase("yes"));
the above condition makes " while" loop run if user enter's yes and will terminate if he enters anything except yes.
When i run my program and try to add a input. It keeps saying invalid input. Enter again. I know why it did that b/c i added a while loop but that was for inputs less than 1. How can I fix this? I need to display the occupancy rate and the amount of vacant rooms. I am a beginner so I think I have made a stupid mistake. Can somebody help me?
import java.util.Scanner;
public class hotelOccupancy
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int floors, numFloors, rooms,totalRoomsOccupied,roomsOccupied, vacant;
double totalRooms;
System.out.println("Enter number of floors");
floors = input.nextInt();
while(floors < 1);
{
System.out.println("Invalid input. Enter again");
}
floors = input.nextInt();
numFloors = input.nextInt();
for(floors = 1; floors <= numFloors; floors++)
{
System.out.println("Enter number of rooms in floor");
rooms = input.nextInt();
while(rooms < 1);
{
System.out.println("Invalid entry. Enter again");
}
System.out.println("Enter number of rooms occupied for floor" + floors);
roomsOccupied = input.nextInt();
totalRoomsOccupied = input.nextInt();
totalRoomsOccupied = totalRoomsOccupied + roomsOccupied;
}
rooms = input.nextInt();
totalRoomsOccupied = input.nextInt();
vacant = rooms - totalRoomsOccupied;
totalRooms = input.nextDouble();
double occupancyRate = totalRoomsOccupied/totalRooms;
System.out.println("The number of rooms vacant are" + vacant);
System.out.println("Then occupancy rate is" + occupancyRate);
You have to move the rooms = input.nextInt(); into the while(rooms < 1) loop.
while(floors < 1); is equivalent to while(floors < 1){} so if you enter a number inferior than one, the loop will never end.
You have to take a new input in the while loop (and remove the ; after the while).
while(floors < 1){
System.out.println("Invalid input. Enter again");
floors = input.nextInt();
}
You have to do the same thing for the other while loop (don't forget to remove the ; after the while too) :
while(rooms < 1){
System.out.println("Invalid entry. Enter again");
rooms = input.nextInt();
}