I'm making a simple while loop when i can make a subtraction between 2 number in java.
The only task of this exercise is this:
Suppose that user insert 2 number by this method (
Scanner keyboard = number.nextInt();
Scanner keyboard2 = number2.nextInt();
Suppose that user insert these 2 number : 8 and 3
I'm not asking for a program which makes 8 - 3 = 5
The program is able to do only substraction or addiction of 1.
so the five is converted in a substraction of -1 for five time.
So instead of 8 - 3, the program calculate 8 -1 -1 -1 -1 -1 = 3
// 8 - 5
Or :
8 -1 = 7
7 - 1 = 6
// ....
4 - 1 = 3
The exercise don't requires complex method, or for loop, only while
As my point of view, i think that you need your answer like your example. Because of that, i made a program for you. In this program if you only enter large number first, you can except if statement, This is my solution.
import java.util.*;
import java.lang.*;
public class Stack2{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
int num1=sc.nextInt();
int num2=sc.nextInt();
if(num1<num2){
System.out.println("Number 1 is less than number 2");
System.exit(1);
}
int x=num1-num2;
System.out.print(num1+" - "+num2+" --> is equal to "+ num1+" " );
while(num1!=x){
System.out.print("-1 ");
num1--;
}
System.out.println("= "+x);
}
}
i'm not sure if you want some like this
int num1 = 8;
int num2 = 5;
int res = num1- num2;
boolean bandera = Boolean.TRUE;
String salida = "";
while(bandera)
{
if(num2 > 0)
{
salida = salida +"-1";
num2--;
}else
{
bandera = Boolean.FALSE;
}
}
System.out.println(num1 + salida + "=" + res);
Your code needs little correction. You haven't declared Scanner object correct and even numbers. Try this code,
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num1 = sc.nextInt();
int num2 = sc.nextInt();
System.out.print(num1 + " - " + num2 + " --> Is equal to " + num1);
while(num2 > 0) {
System.out.print(" - 1");
num1 -= 1;
num2--;
}
System.out.println(" = " + num1);
sc.close();
}
Related
I was wondering when I tried to print the value of recursion in main, the answer was:
Enter the number: 1
2The result is:
How to make the number 2 to the front like,
The result is: 2
import java.util.Scanner;
public class Question4Final {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter the number: ");
int a = scan.nextInt();
System.out.printf("The result is: ", multiplication(a));
}
public static int multiplication(int a) {
if (a == 5) {
int multiply = 10 * 6 * 2;
System.out.print(multiply);
} else if (a == 4) {
int multiply2 = 6 * 2;
System.out.print(multiply2);
} else if (a == 1) {
System.out.print("2");
}
return a;
}
}
To call the method:
System.out.printf("The result is: ", multiplication(a));
first the arguments must be evaluated, so multiplication(a) is executed before System.out.printf("The result is: ", multiplication(a)). Since multiplication(a) prints something, that printing takes place before "The result is:" is printed.
You should change multiplication(a) to simply return the result without printing it. Then use System.out.println("The result is: " + multiplication(a)) to print the result.
Note the you have to change the value returned by multiplication(a), since currently you return a, which is not the value printed by that method.
You have 2 issues in your code.
First is you are printing the value of 'multiply' in your static method :
public static int multiplication(int a){
System.out.print(multiply);
That is a reason why it is printing 2 before the statement :
2The result is:
2nd issue is you are calling the method multiplication in the print statement :
System.out.printf("The result is: ", multiplication(a));
That is not how to print the result by calling the method.
I have taken your example and run the below code. You can check this code.
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter the number: ");
int a = scan.nextInt();
int product = multiplication(a);
System.out.println("The result is : " +product);
}
public static int multiplication(int a){
int multiply = 0;
if(a == 5){
multiply = 10 * 6 * 2;
}else if(a == 4){
multiply = 6 * 2;
}else if(a == 1){
multiply = 2;
}
return multiply;
}
}
Below are the outputs on different options :
Enter the number: 4
The result is : 12
Enter the number: 5
The result is : 120
Enter the number: 1
The result is : 2
Why does the second while loop while (numberOfTries < 2) cancel both while loops? It runs perfect if there is no incorrect answer. But let's say I select 4 problems to be made, and I am only on the first problem. I give the incorrect answer 2 times so the program should say Incorrect two times and then give me a new question because while (numberOfTries < 2) should force it to break from that loop. But it doesn't, it just quits the whole thing. I know it has to be a logic issue, so what am I missing?
import java.util.Random;
import java.util.Scanner;
public class Howthe {
public static void main(String[] args) {
// Open Scanner
Scanner scan = new Scanner(System.in);
// Ask user to choose number of problems to be made.
// Can only choose 4, 9, or 16
System.out.print("Choose a number of problems to be made (4, 9, 16): ");
int userChoiceOfProblems = scan.nextInt();
// Ask user to choose a number between 0 and 12
System.out.print("\nChoose a number between 0 and 12: ");
int userNumberBetween0and12 = scan.nextInt();
// Ask user to choose between add/sub or multiply/divide
System.out.println("\nChoose to:"
+ "\n0: add/sub your chosen number"
+ " and the randomly generated number: "
+ "\n1: multiply/divide your chosen number"
+ " and the randomly generated number: ");
int userArithmeticChoice = scan.nextInt();
int counter = 0;
String equationString;
int equationAnswer;
int numberOfAnswersRight = 0;
int numberOfTries = 0;
int userAnswerToQuestion;
if (userArithmeticChoice == 0){
while (counter < userChoiceOfProblems){
// Create random number to decide if add or sub used.
// add is equal to 0 and sub is equal to 1
Random rand = new Random();
int randomNumberBetween0and1 = rand.nextInt(1) + 0;
// Create random number that is multiplied by userNumberBetween0and12
int randomNumberBetween0and12 = rand.nextInt(12) + 0;
// Add and increase counter by 1
if (randomNumberBetween0and1 == 0){
// If numberOfTries is more than 2, then display answer.
while (numberOfTries < 2){
// Compute the right answer (addition).
equationAnswer = userNumberBetween0and12 + randomNumberBetween0and12;
// Produce string of equation, then display string (addition).
equationString = userNumberBetween0and12 + " + "
+ randomNumberBetween0and12;
System.out.println(equationString);
userAnswerToQuestion = scan.nextInt();
// If answer is right, increase numberOfAnswersRight.
if (userAnswerToQuestion == equationAnswer){
numberOfAnswersRight++;
System.out.println("Correct!");
break;
}
// If answer is wrong, continue loop and increase numberOfTries
else if (userAnswerToQuestion != equationAnswer){
numberOfTries++;
System.out.println("Incorrect");
}
} // end of while (numberOfTries < 2 && !quit)
counter++;
}
} System.out.println("Yout got " + numberOfAnswersRight + " problem(s) right!");
}
}
}
numberOfTries is initialized outside of your loops. Once you try twice, it never gets set back to 0 which causes the loops to skip and finish on the next question because numberOfTries is already 2.
I'm learning java. I suspect my compute_Hailstone_Sequence method terminates upon hitting a return statement. With the two inputs of begin_num = 7 and steps = 10 my ideal output should represent 7 22 11 34 17 52 26 13 40 20 I also suspect I am not yet incrementing x as my code DOES produces the first two values 7 22 ..I am not sure it can yet produce a cumulative algorithm. I am aware of answers using data structures but I am trying to code this without using lists or arrays or any other data structure. This is not homework.
import java.util.Scanner;
/**
* Created on 9/5/15.
* Following Reges, Stuart, and Martin Stepp. Building Java Programs: A Back to Basics Approach. 3rd Edition.
* Chapter 4
*/
public class Ch4 {
public static void main(String[] args) {
hailstone_Sequence();
}
public static void hailstone_Sequence(){
giveIntro();
Scanner user_console = new Scanner(System.in);
System.out.println("Please provide a starting integer: ");
int BEGINVALUE = user_console.nextInt();
// System.out.println("Please provide an ending integer: ");
// int ENDVALUE = user_console.nextInt();
System.out.println("Thank you. How long would you liked the sequence to be?");
int STEPS = user_console.nextInt();
System.out.println("Calculating..");
compute_Hailstone_Sequence(BEGINVALUE, STEPS);
}
public static int compute_Hailstone_Sequence(int begin_num, int steps){
System.out.print(begin_num + " ");
for (int i = 1; i <= steps; i++ ){
int x;
if ((begin_num & 1) == 0 ){
// even
// int x = 0;
x = (begin_num / 2);
System.out.print(x + " ");
// return x;
}
else {
// int x = 0;
x = (3 * begin_num + 1);
System.out.print(x + " ");
// return x;
}
return x;
}
return begin_num;
}
}
Try this, I do not know if that's what you are looking for, me are based on your output.
public static void hailstone_Sequence(){
Scanner user_console = new Scanner(System.in);
System.out.println("Please provide a starting integer: ");
int BEGINVALUE = user_console.nextInt();
System.out.println("Thank you. How long would you liked the sequence to be?");
int STEPS = user_console.nextInt();
System.out.println("Calculating..");
compute_Hailstone_Sequence(BEGINVALUE, STEPS);
}
public static void compute_Hailstone_Sequence(int begin_num, int steps){
System.out.print(begin_num + " ");
for (int i = 1; i < steps; i++ ){
if (begin_num%2 == 0 ){
begin_num = (begin_num / 2);
System.out.print(begin_num + " ");
}
else {
begin_num = (3 * begin_num) + 1;
System.out.print(begin_num + " ");
}
}
}
Output is:
Please provide a starting integer:
7
Thank you. How long would you liked the sequence to be?
10
Calculating..
7 22 11 34 17 52 26 13 40 20
import java.util.Scanner;
/**
* Created by on 9/5/15.
* Following Reges, Stuart, and Martin Stepp. Building Java Programs: A Back to Basics Approach. 3rd Edition.
* Chapter 4
*/
public class Ch4 {
public static void main(String[] args) {
hailstone_Sequence();
}
public static void hailstone_Sequence(){
giveIntro();
Scanner user_console = new Scanner(System.in);
System.out.println("Please provide a starting integer: ");
int BEGINVALUE = user_console.nextInt();
// System.out.println("Please provide an ending integer: ");
// int ENDVALUE = user_console.nextInt();
System.out.println("Thank you. How long would you liked the sequence to be?");
int STEPS = user_console.nextInt();
System.out.println("Calculating..");
compute_Hailstone_Sequence(BEGINVALUE, STEPS);
}
public static int compute_Hailstone_Sequence(int begin_num, int steps) {
// expected output = 7 22 11 34 17 52 26 13 40 20 10
System.out.print(begin_num + " ");
int x = 0;
for (int i = 1; i <= steps; i++) {
if ((begin_num & 1) == 0) {
// even
// int x = 0;
x = (begin_num / 2);
System.out.print(x + " ");
// return x;
begin_num = x;
} else {
// int x = 0;
x = (3 * begin_num + 1);
System.out.print(x + " ");
// return x;
begin_num = x;
}
}
return x;
}
public static void giveIntro(){
System.out.println("In mathematics, there is an open problem that involves\n " +
"what are known as hailstone sequences.\n" +
"These sequences of numbers often rise and\n " +
"fall in unpredictable pattern, which is somewhat\n" +
" analogous to the process that forms hailstones.\n");
System.out.println("This method outputs hailstone sequences:");
}
}
I am working on this project from the Java Programming book by Joyce Farrell, and I am having an issue with the Randomly Generated number and the user's guesses not being checked correctly. For example the user has 3 guesses, lets say their first guess it 2 and the first randomly generated number is 2 the program will print out You lose. When the guess is actually correct. Please help me. I have added the details of the program plus what I have done so far.
Create a lottery game application. Generate three random numbers (see Appendix D for help in
doing so), each between 0 and 9. Allow the user to guess three numbers. Compare each of the
user's guesses to the three random numbers and display a message that includes the user's
guess, the randomly determined three-digit number, and the amount of money the user has
won as follows.
Matching Numbers Award($)
Any one matching 10
Two matching 100
Three matching, not in order 1000
Three matching, in exact order 1,000,000
No match 0
Make certain that your application accommodates repeating digits. For example, if a user
guesses 1, 2, and 3, and the randomly generated digits are 1, 1, and 1, do not give the user
credit for three correct guesses - just one. Save the file as Lottery.
My Source Code
// Filename: Lottery.java
// Written by: Andy A
// Written on: 14 January 2015
import java.util.Scanner;
import java.util.Random;
public class Lottery {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
Random ranNum = new Random();
// LIMIT Contains The Numbers From 0 - 9
// TIMES Contains The Number of Time ranNum Should Run
final int LIMIT = 9;
final int TIMES = 3;
// Users Guesses
int usersFirstGuess;
int usersSecondGuess;
int usersThirdGuess;
// Randomly Generated Numbers
final int GenFirst = ranNum.nextInt(LIMIT);
final int GenSecond = ranNum.nextInt(LIMIT);
final int GenThird = ranNum.nextInt(LIMIT);
// User is asked for 3 guesses
System.out.println("Please enter your first guess: ");
usersFirstGuess = userInput.nextInt();
System.out.println("Please enter your second guess: ");
usersSecondGuess = userInput.nextInt();
System.out.println("Please enter your third and final guess: ");
usersThirdGuess = userInput.nextInt();
// Winning Amounts
final double WinTen = 10;
final double WinHun = 100;
final double WinThund = 1000;
final double WinMillion = 1000000;
final int WinZero = 0;
// Shows the randomly generated numbers
for(int x = 0; x < TIMES; ++x)
System.out.print(ranNum.nextInt(LIMIT) + " ");
System.out.println();
// First Generated
if(GenFirst == usersFirstGuess ) {
System.out.println("You have won: $" + WinTen);
}
else if(GenSecond == usersSecondGuess) {
System.out.println("You have won: $" + WinTen);
}
else if(GenThird == usersThirdGuess) {
System.out.println("You have won: $" + WinTen);
}
}
}
You are printing newly generated numbers with ranNum.nextInt(LIMIT), however you are comparing the user input with the numbers stored in the GenXXX variables.
Solution: Print the variables instead.
System.out.println(GenFirst + " " + GenSecond + " " + GenThird);
If you still want to use a loop for printing you can store the numbers in an array.
// generate
final int[] generated = new int[TIMES];
for (int x = 0; x < TIMES; x++)
generated[x] = ranNum.nextInt(LIMIT);
// print
for (int x = 0; x < TIMES; x++)
System.out.print(generated[x] + " ");
This should do the trick.
// Filename: Lottery.java
// Written by: Andy A
// Written on: 14 January 2015
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Random;
public class Lottery {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
Random ranNum = new Random();
// LIMIT Contains The Numbers From 0 - 9
// TIMES Contains The Number of Time ranNum Should Run
final int LIMIT = 9;
final int TIMES = 3;
// Users Guesses
int usersFirstGuess;
int usersSecondGuess;
int usersThirdGuess;
List<Integer> guesses = new ArrayList<>();
// Randomly Generated Numbers
final int GenFirst = ranNum.nextInt(LIMIT);
final int GenSecond = ranNum.nextInt(LIMIT);
final int GenThird = ranNum.nextInt(LIMIT);
// User is asked for 3 guesses
System.out.println("Please enter your first guess: ");
usersFirstGuess = userInput.nextInt();
guesses.add(usersFirstGuess);
System.out.println("Please enter your second guess: ");
usersSecondGuess = userInput.nextInt();
guesses.add(usersSecondGuess);
System.out.println("Please enter your third and final guess: ");
usersThirdGuess = userInput.nextInt();
guesses.add(usersThirdGuess);
// Winning Amounts
final double WinTen = 10;
final double WinHun = 100;
final double WinThund = 1000;
final double WinMillion = 1000000;
final int WinZero = 0;
// Shows the randomly generated numbers
System.out.println(GenFirst + " " + GenSecond + " " + GenThird);
List<Integer> lottery = new ArrayList<>();
lottery.add(GenFirst);
lottery.add(GenSecond);
lottery.add(GenThird);
if (guesses.equals(lottery)) {
System.out.println("You have won: $" + WinMillion);
} else {
int matchCount = 0;
for (Integer guessValue : guesses) {
if (lottery.contains(guessValue)) {
matchCount++;
lottery.remove(guessValue);
}
}
switch (matchCount) {
case 0:
System.out.println("You have won: $" + WinZero);
break;
case 1:
System.out.println("You have won: $" + WinTen);
break;
case 2:
System.out.println("You have won: $" + WinHun);
break;
case 3:
System.out.println("You have won: $" + WinThund);
break;
}
}
}
}
Exactly,
why are you printing
System.out.print(ranNum.nextInt(LIMIT) + " ");
when you should be just printing
System.out.print(GenThird + " ");
System.out.print(GenSecond + " ");
System.out.print(GenFirst + " ");
This is not the problem of the randomly generated numbers, but if your way of showing them to the user.
Before your if / else if statements, in the for-loop you are generating new random numbers. That means, the number compared to the users input (genFirst) can be 3, but the number shown to the user in the for loop is a new random number, for example 2.
To fix this problem, you should display the generated numbers like that:
for (int ranInt : new int[] { GenFirst, GenSecond, GenThird}) {
System.out.println(ranInt);
}
This piece of code creates an array of the generated numbers and loops through them printing them. Obviously, you can also print GenFirst, then print GenSecond and then print GenThird.
I hope this helps!
Maybe this will help!
import java.util.Scanner;
import java.util.Random;
public class Qellonumrat {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Random rand=new Random();
int random_integer=(int) rand.nextInt(10);
System.out.println("Guess the number: ");
int number=sc.nextInt();
while(true){
if(number == random_integer){
random_integer++;
System.out.println("Congrats you won!!!");
break;
}
else{
System.out.println("Try again");
break;
}
}
}
}
My name is Fermin. I'm new in this forum and I'm also studying Java to be a Java developer. I'm stuck on an assignment and I would like some help from anyone. Here is the description and the code
Using an if statement in the for block, determine
whether randNum and guessNum are equal.
public class GuessGame {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
int randNum , guessNum ;
//Generates a random number from 1 to 10
randNum = new java.util.Random().nextInt(10) + 1;
System.out.println("Im thinking of a number from 1 to 10");
for (guessNum = 0; guessNum <= 10; guessNum ++){
java.util.Scanner scan = new java.util.Scanner(System.in);
guessNum = scan.nextInt();
if (guessNum == randNum) {
System.out.println("you guess" + guessNum );
}
}
}
}
Updated code
import java.util.Random;
import java.util.Scanner;
public class NumberGuess {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Random rand = new Random();
System.out.println("Im thinking of a number from 1 to 10");
int number = scan.nextInt(10);
//Generates a random number from 1 to 10
int number2 = rand.nextInt(10)+1;
System.out.println("you enter the number" + " " + number);
for (int counter = -1; counter < 3; counter ++ ){
if(number!= number2)
System.out.println("and your random number is:" + " " + number2 + " " + "please try again");
else
System.out.println("your guess number is equal to the random number Good job guessing");
break;
}
}
}
Your for loop is controlled by the guessNum variable. Within the loop, you're reassigning that variable's value when you do this:
guessNum = scan.nextInt();
As a result, you're likely getting an inconsistent number of loops because you're changing the variable that the loop relies on for control.
Given the name of this variable, I suspect you originally intended it to be used to store the user's input. If that is the case, I would alter your for loop to use a different variable instead, like so:
for (int guessCount = 0; guessCount < 10; guessCount++){
Also note, your condition originally had <= 10; since your counting variable started at zero, this will allow the user 11 guesses, rather than 10 (since 0 to 10 inclusive = 11). Assuming you wanted 10 guesses instead, you will want to check for less than 10 (since 0..10 exclusive = 10).
import java.util.Random;
import java.util.Scanner;
public class NumberGuess {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Random rand = new Random();
System.out.println("Im thinking of a number from 1 to 10");
int number = scan.nextInt(10);
//Generates a random number from 1 to 10
int number2 = rand.nextInt(10)+1;
System.out.println("you enter the number" + " " + number);
for (int counter = -1; counter < 3; counter ++ ){
if(number!= number2)
System.out.println("and your random number is:" + " " + number2 + " " + "please try again");
else
System.out.println("your guess number is equal to the random number Good job guessing");
break;
}
}
}