how do i print out the age multiplied by 3 [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want the age to be multiplied by 3 here is my code asking for the user to enter name and age...............................................
import java.util.Scanner;
class Lab1Part4 {
public static void main (String [] args) {
Scanner user_input =
int tripleAge;
String printMyName;
String firstName;
String secondName;
System.out.println("Please enter your first name ? ");
firstName = user_input.next ();
System.out.println("Please enter your second name? ");
secondName = user_input.next ();
printMyName = firstName + " " + secondName;
System.out.println( printMyName);
System.out.println("Enter your age ? ");
tripleAge = user_input.nextInt ();
}// end class
}// end main

You just need to do something like:
tripleAge = user_input.nextInt () * 3;
System.out.println(tripleAge);

Use * operator to multiply.
tripleAge = (user_input.nextInt () * 3);
System.out.println(tripleAge);

Related

error cannot convert int to boolean in the if else if statement in java [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 months ago.
Improve this question
package Fare;
import java.util.Scanner;
public class fare {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print(" Enter Class ");
System.out.println(" a. Senior b. Student c. Regular ");
System.out.println("Enter class : ");
int num = input.nextInt();
if (num = 1) {
double fare = 9 * 0.10;
System.out.println("Your fare will be " + fare + "ā–’. Thank you.");
}
else if (num = 2 ) {
double fare = 9 * 0.08;
System.out.println("Your fare will be " + fare + "ā–’. Thank you.");
}
else {
System.out.println("your fare will be 9ā–’. Thank you.");
}
}
}
Hi i can't seem to understand why there is an error in my if statement stating that it cannot convert int to boolean. I have already checked it and still can find any problem or mybe i just don't know.
You should change if(num=1) to if(num==1) and if else(num=2) to if else(num==2).

Guessing Game for Java (Print Different Statement Loop) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am not sure how to exactly ask my question, but I wanted to print the statement "your first/second/third try" every time the user guesses ( I think what I commented in my code will seem clearer than what I am trying to convey now) but I'm confused on how to do so. also, sorry if my code is messy, I'm a newbie at this lol.
import java.util.Scanner;
import java.util.Random;
import java.lang.System;
public class GuessingGame {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
Random randGen = new Random();
int userGuess = 0;
int userTries = 0;
int userCompRandom = randGen.nextInt(11)+1;
String userName = "";
boolean isWrong = true;
string guessNumberTag = "";
System.out.println("Welcome to the number guessing game. What's
your name?");
userName = scnr.nextLine();
System.out.println("Iā€™m thinking of an integer between 1 and 10.
You have 3 guesses.");
while (isWrong = true){
userTries +=1;
userGuess = scnr.nextInt();
if (userGuess > userCompRandom){
System.out.println(userGuess);
System.out.println("Too high, guess lower!");
}
else if (userGuess < userCompRandom){
System.out.println(userGuess); **//First/second/third
try: userGuess//**
System.out.println("Too low, guess higher!");
}
else if (userGuess == userCompRandom){
System.out.println(userGuess);//First/second/third try: userGuess//
System.out.println("Congratulations " + userName + "! It took you " + userTries + "!");
break;
}
if (userTries>4){
System.out.println("Game over " +userName + ",you lose!:p");
break;
}
}
}
}
String[] guessStatement = {"Your first try.", "Your second try.", "Your third try."};
System.out.println(guessStatement[userTries]);
Array index starts at 0, so adjust userTries.
You could create a method to do this. Note your check is >4 so I added fourth.
private static String getPrompt(int tries, int guess) {
String[] tryWords = { "first", "second", "third", "fourth"} ;
return String.format("Your %s try: %d", tryWords[tries-1], guess );
}
and call it:
System.out.println(getPrompt(userTries, userGuess )); //**First/second/third try: userGuess//**

What does" .class expected error " mean in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Here is my code. I got an error as ".class expected".
What should I do to rectify it.
import java.util.*;
class Atm2
{
public static void main (String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the amount to be withdrawed");
int withdraw = in.nextInt();
System.out.println("Enter the amount in the account");
double acnt_balc = in.nextDouble();
if ((withdraw % 5 == 0) && (acnt_balc >= withdraw + 0.50))
double cur = acnt_balc - (withdraw + 0.50);
System.out.println(cur);
else
System.out.println(acnt_balc);
}
}
Try curly braces in the context of your if-else.
It should look like this:
if (isMoving) {
currentSpeed--;
} else {
System.err.println("The bicycle has already stopped!");
}
So you can see, that there is a if-block and a else-block. You have to say which code belongs to if and which belongs to else. You can do that with the braces.
Scanner requires you to import java.util.*;
I tried it works and also format properly , see below , it runs
import java.util.*;
class Atm2
{
public static void main (String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the amount to be withdrawed");
int withdraw = in.nextInt();
System.out.println("Enter the amount in the account");
double acnt_balc = in.nextDouble();
if ((withdraw % 5 == 0) && (acnt_balc >=(withdraw + 0.50)))
{
double cur=(acnt_balc-(withdraw + 0.50));
System.out.println(cur);
}
else
{
System.out.println(acnt_balc);
}
}
}
Also make sure your Filename is the same as class name , in this case Atm2.java
The Scanner Class in present in the 'java.util' package , since you forgot to import it and used Scanner sc=new Scanner(); the compliler is complaining, and remember to compile use javac filename.java , to run use java filename

String Var with int var [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to have a string variable showing next to my int variable but it doesn't seem to work.
public class ThreeLittleNumbers {
public static void main(String[] argts) {
String as = "Number 1: ";
String bs = "Number 2: ";
String cs = "Number 3: ";
int a = 1;
int b = 3;
int c = 5;
String tot = "Total: ";
System.out.println(as+a);
System.out.println(bs+b);
System.out.println(cs+c);
System.out.println(tot);
System.out.print(a+b+c);
}
}
If I understand your question, then this
System.out.println(tot);
System.out.print(a + b + c);
should be
System.out.print(tot);
System.out.println(a + b + c);
When I make the change above, I get output like you seem to be asking for -
Number 1: 1
Number 2: 3
Number 3: 5
Total: 9
Another option would be to use printf and something like
System.out.printf("%s %d%n", tot, a + b + c);

How do you split an integer in java? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have tried making the integer into a string then splitting it like that but then I get an error because I have to multiply that number that is a string but obviously I can't. Here is my code.
import java.util.Scanner;
public class BMI {
public static void main(String[] args) {
Scanner BMI1 = new Scanner(System.in);
Scanner BMI2 = new Scanner(System.in);
Scanner BMI3 = new Scanner(System.in);
String name;
int weightPounds;
int heightFtInches;
int heightInches;
double weightKg;
double heightM;
System.out.print("Please enter your name (e.g. First Last): ");
name = BMI1.nextLine();
System.out.print("Please enter your weight in pounds (e.g. 150): ");
weightPounds = BMI2.nextInt();
System.out.print("Please enter your height in feet and inches (e.g. 6, 8): ");
heightFtInches = BMI3.nextLine();
String[] token = heightFtInches.split(",");
System.out.println();
System.out.println("Body Mass Index Calculator");
System.out.println("#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#");
System.out.println();
weightKg = weightPounds / 2.2;
heightInches = heightFtInches * 12;
System.out.println("Name: " + name);
System.out.println("Weight in Kg: " + weightKg);
System.out.println("Height in meters: " + heightInches);
}
}
The nextInt method can be used to read the values for feet and weight. The method will use a space as a delimiter even if the values are entered simultaneously:
int feet = bmi1.nextInt();
int height = bmi1.nextInt();
Here you split the String and don't use it (well after you change heightFtInches to a String)
String[] token = heightFtInches.split(",");
What you want is to convert each into an int (Note this is assuming valid input)
int feet = Integer.parseInt(token[0]);
int inches = Integer.parseInt(token[1]);
Then you can mutliple feet and inches by the correct equatiob to get metric height
Adding to the previous two answers, you have declared heightFtInches as an int but I think it should be a String so you can use the split() method on it.
String[] token = heightFtInches.split(",");
System.out.println();
System.out.println("Body Mass Index Calculator");
System.out.println("#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#");
System.out.println();
weightKg = weightPounds / 2.2;
heightInches = heightFtInches * 12;
Your height in feet is now in token[0] so I assume you are looking for something like this:
heightInches = (Integer.parseInt(token[0]) * 12) + Integer.parseInt(token[1]);

Categories

Resources