String Var with int var [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 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);

Related

local variable initialization were wrong 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 1 year ago.
Improve this question
I am new to java, why a, b,c initialization are wrong in the following code.
public static void main(String[] args) {
if (args.length < 2)
throw new IllegalArgumentException ("we need 2 argumeents");
else {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = a+b;
}
System.out.println(a + " + " + b + " = " + c);
}
Java works differently compared to JavaScript. Every {} block has an own variable scope. Variables defined inside a block are not visible outside.
public static void main(String[] args) {
{
int x=1;
System.out.println(x); // prints 1
}
{
int x=2;
System.out.println(x); // prints 2
}
// System.out.println(x); // error: cannot find symbol
}

Is it possible to use a String if statement to change an integer? [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 1 year ago.
Improve this question
I'm just having a practise at some java coding and was wondering if there was a way to use a String if else statement to change change an integer, as the following doesn't like the c = a * b
int a, b, c;
Scanner sc = new Scanner(System.in);
System.out.println("Enter first number");
a = sc.nextInt();
System.out.println("Enter * + - or / ");
String d = sc.nextLine();
System.out.println("Enter second number");
b = sc.nextInt();
if (d.equals("*")){
c = a * b;
}
System.out.println(a + " " + d + " b " + " = " + c);
This happens because the variable "c" is initialized only if the conditional if (d.equals("*")) {...} is true, to solve this you have two options:
Just initialize "c" :
int a, b, c = 0;
or add else statement
if (d.equals("*")){
c = a * b;
}else {
c = 0;
}

Converting these to Java? [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 2 years ago.
Improve this question
I need to convert these to Java:
Ive tried converting line by line for both and when I run it there is no output when it completes
def count_up_from (a,b):
start = a
end = b
count = start
while count <= end:
print(count , end = ',')
count = count + 1
return count
print(count_up_from(1,5), end = "," "\n")
print(count_up_from(3,6), end = "," "\n")
print(count_up_from(1,2), end = "," "\n")
//
def count_down_to (a,b):
start = a
end = b
count = start
while count >= end:
print(count , end = ',')
count = count - 1
return count
print(count_down_to(5,1), end = "," "\n")
print(count_down_to(6,3), end = "," "\n")
print(count_down_to(2,1), end = "," "\n")
//
I did a rough translation here of count_up_from
Your desired outcome is unclear, so its not possible to do perfectly. This should be enough to help you translate the second.
https://repl.it/repls/CoralOblongSyndrome
class Main {
public static void main(String[] args) {
countUpFrom(1,5);
System.out.println();
countUpFrom(3,6);
System.out.println();
countUpFrom(1,1);
System.out.println();
}
public static void countUpFrom(int a, int b) {
for (int i = a; i <= b; i++) {
System.out.print(i + ",");
}
}

How do I format the output decimal to two places after the decimal 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 2 years ago.
Improve this question
I want my fallingTime numbers to be limited to two decimal places. I used "String.format(%.2f)" but it is giving me an error message.
This is the error message:
Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.String
at java.base/java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4426)
at java.base/java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2951)
at java.base/java.util.Formatter$FormatSpecifier.print(Formatter.java:2898)
at java.base/java.util.Formatter.format(Formatter.java:2673)
at java.base/java.util.Formatter.format(Formatter.java:2609)
at java.base/java.lang.String.format(String.java:3302)
at fallingDistance.main(fallingDistance.java:22)
public class fallingDistance {
public static double fallDistance(int fallingTime){
double distance;
double g = 9.8;
distance = (0.5*g*Math.pow(fallingTime, fallingTime));
return distance;
}
//public static void displayHeader(){
//System.out.println("Time\t\t\t\t\tDistance\n----\t\t\t\t\t--------");
//}
public static void main(String[] args){
//displayHeader();
for(int time = 1; time <= 10; time++){
System.out.println(time+ " seconds\t\t" + String.format("%.2f", fallDistance(time) + " meters"));
}
}
}
You had misplaced a bracket. The below should work.
for (int time = 1; time <= 10; time++) {
System.out.println(time + " seconds\t\t" + String.format("%.2f", fallDistance(time)) + " meters");
}
Replace + with ,
System.out.println(time + " seconds\t\t" + String.format("%.2f", fallDistance(time), " meters"));
I removed " meters" from the end of the code and put it after "%.2f" and that worked. The code looks like this now:
{System.out.println(time + " seconds\t\t" + String.format("%.2f meters", fallDistance(time)));}

how do i print out the age multiplied by 3 [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 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);

Categories

Resources