Why does not this code run? - java

In the code you need to take 100 straight-angle triangles and print the biggest triangle. What am I not doing right?
import java.util.Scanner;
public class rthji {
/**
* #param args
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = 0;
int num1 = 0;
int num2 = 0;
int num3 = 0;
int num4 = 0;
int num5 = 0;
System.out.println("Insert the length of the small side of the triangle");
num = in.nextInt();
System.out.println("Insert the length of the big of the triangle");
num1 = in.nextInt();
System.out.println("Insert the length of the last side of the triangle");
num2 = in.nextInt();
for (int i = 1; 1 >= 10; i++) {
System.out.println("Insert the length of the small side of the triangle");
num3 = in.nextInt();
System.out.println("Insert the length of the big of the triangle");
num4 = in.nextInt();
System.out.println("Insert the length of the last side of the triangle");
num5 = in.nextInt();
if (num3 * num3 + num5 * num5 != num4 * num4) {
num3 = num4 = num5 = 0;
} else if (num3 > num && num1 < num4 && num2 < num5) {
num = num3;
num1 = num4;
num2 = num5;
}
System.out.println("The ribs of the largest triangle are:" + num1 + (",") + num2 + (",") + num3);
}
}
}

Your for loop has a error and it should be changed to
for(int i=1;1<=10;i++)
In your case you are using 1 >= 10 which is logically wrong so your loop worries that it cannot go ahead because 1 is less than 10

Change your for loop like below
for(int i=1; i<=10; i++)

Related

Rookie Java question - Scanner, int and user input

Hello there fellow Overflowers!
I'm getting started with Java and want to create a small program where it's possible to type in 5 numbers and get the sum and average printed out.
The program runs and does it's job, but I feel that there must be a way smarter method / way then what I've done.
import java.util.Scanner;
public class Calc {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("5 random numbers");
int num1 = input.nextInt();
int num2 = input.nextInt();
int num3 = input.nextInt();
int num4 = input.nextInt();
int num5 = input.nextInt();
int sum = num1 + num2 + num3 + num4 + num5;
int avg = (num1 + num2 + num3 + num4 + num5)/5;
System.out.println("Sum is" + sum + " and the average is " + avg);```
Is it possible to declare all the numbers in one line or something?
You can use BufferedReader. Try this. Here, you need to mention all numbers in a single line.
Ex: 1 2 3 4 5
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] values = line.split(" ");
int[] arr = new int[values.length];
int sum=0;
for (int i = 0; i < values.length; i++) {
arr[i] = Integer.parseInt(values[i]);
sum+=arr[i];
}
double avg = sum/arr.length;
System.out.println("Sum is" + sum + " and the average is " + avg);

How do you write the conditions for the middle number when sorting 3 numbers in ascending order using if-else statements in java?

I've been trying different conditions but I can't seem to get it right. When I type 54, 53, 57, the output is correct. But when I type the same numbers in a different order, the output becomes 53, 0 , 57. Help please
This is my code for the middle number. I can't find out what's wrong.
if (num1<num2 && num1>num3){ //for middle number
secNum = num1;
}else if (num2<num1 && num2>num3){
secNum = num2;
}else if (num3<num1 && num3>num2){
secNum = num3;
}
Try this code if you're familiar with arrays
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int num1 = s.nextInt();
int num2 = s.nextInt();
int num3 = s.nextInt();
int arr[] = {num1,num2,num3};
Arrays.sort(arr);
System.out.println(arr[1]);
}
}
This Will Work
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int num1 = s.nextInt();
int num2 = s.nextInt();
int num3 = s.nextInt();
int secNum=0;
if(num2>num1 && num1>num3 || num3>num1 && num1>num2){
secNum=num1;
}
if(num1>num2 && num2>num3 || num3>num2 && num2>num1){
secNum=num2;
}
if(num1>num3 && num3>num2 || num2>num3 && num3>num1){
secNum=num3;
}
System.out.print(secNum);
}
}
Try this:
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int num1 = s.nextInt();
int num2 = s.nextInt();
int num3 = s.nextInt();
int secNum = 0;
if (num1 < num2 && num1 < num3) {
secNum = Math.min(num2, num3);
} else if (num2 < num1 && num2 < num3) {
secNum = Math.min(num1, num3);
} else if (num3 < num1 && num3 < num2) {
secNum = Math.min(num1, num2);
}
System.out.println(secNum);
}
}
You have 3 numbers, which can be in 6 different orders:
num1, num2, num3
num1, num3, num2
num2, num1, num3
num2, num3, num1
num3, num1, num2
num3, num2, num1
But your code has only 3 if-cases, which check for 3 possible orders.
That is why 3 out of 6 cases are wrong.
(If numbers can have the same value, you need to check with <= or >=)
So to keep you algorithm style, it has to be like this:
public static void main(String[] args) {
int num1 = 59;
int num2 = 57;
int num3 = 55;
int secNum = 0;
if (num1 < num2 && num1 < num3) {
if (num2 < num3)
secNum = num2;
else
secNum = num3;
} else if (num2 < num1 && num2 < num3) {
if (num1 < num3)
secNum = num1;
else
secNum = num3;
} else if (num3 < num1 && num3 < num2) {
if (num1 < num2)
secNum = num1;
else
secNum = num2;
}
System.out.println(secNum);
}
But Java has a lot of better ways to sort numbers, so I can not recommend this approach.

Fibonnaci sequence explanation

Can someone explain to me some parts of this code:
public static void sequence(int nterms){
int num1 = 0;
int num2 = 1;
int num = 2;
if (nterms <= 0){
System.out.println("Enter a positive integer");
}
else if (nterms == 1){
System.out.println(" fibonacci sequence: " + num1);
}
else{
System.out.println(num1 );
System.out.println(num2);
while (num < nterms){
int nth = num1 + num2;
System.out.println(nth);
num1 = num2;
num2 = nth;
num++;
}
}
}
The number sequence that's in the output is correct. So the code works. But why do you do num++ in the end? I know nth is previous two numbers added together but why do you nterms == 1 and print "" +num1? I don't get that.
Here it is
public static void sequence(int nterms){
/*instancation of needed variables : nth = num1+numb2 later in the code*/
int num1 = 0;
int num2 = 1;
/*num is the number of term you've encoutered, as the first two are in
the initialization, you start at 2*/
int num = 2;
if (nterms <= 0){ //check wether the function has a correct input or not*/
System.out.println("Enter a positive integer");
}
else if (nterms == 1){ /*if it's the first term we know in num1*/
System.out.println(" fibonacci sequence: " + num1);
}
else{
System.out.println(num1);/*we print the first two terms*/
System.out.println(num2);
while (num < nterms){ /*we loop till we reached the wanted number*/
/*we know a term is equal to the two terms before it, at least I hope you do*/
int nth = num1 + num2;
System.out.println(nth); /*we've got our number so we print it*/
num1 = num2; /*we make sure num1 and num2 are the last two encountered, so num1 becomre num2 and num2 become the current term (nth)*/
num2 = nth;
num++;/*we increment the variable as we've met printed a new term*/
}
}
}

Fibonacci number not starting where it should

I'm attempting to utilize a for loop in order to make a fibonacci sequence and unfortunately when I run the method (listed below), it does not start at 0 as it should. The first number is always 2 when it should be 0. If anyone could explain to me how to make the sequence begin with 0, that would be great. Thank you.
public static final void fibonacci(){
int num = 1;
int num2 = 1;
int tnum = 0;
int startnum = 1;
System.out.println("Please input a number:");
Scanner input = new Scanner(System.in);
int x = input.nextInt();
for(int i = 0; i < x; i++){
System.out.print(num + num2 + " ");
tnum = num;
num = num2;
num2 = tnum + num2;
}
}
You should start with
num = 0
and then print num, and modifying num (then in your i-th loop, num is the i-th fibonacci number :
public static final void fibonacci() {
int num = 0;
int num2 = 1;
int tnum = 0;
int startnum = 1;
System.out.println("Please input a number:");
Scanner input = new Scanner(System.in);
int x = input.nextInt();
for (int i = 0; i < x; i++) {
System.out.print(num);
tnum = num + num2;
num = num2;
num2 = tnum;
}
}
public static final void fibonacci(){
int num = -1; //was "1"
int num2 = 1;
int tnum = 0;
int startnum = 1;
System.out.println("Please input a number:");
Scanner input = new Scanner(System.in);
int x = input.nextInt();
for(int i = 0; i < x; i++){
System.out.print(num + num2 + " ");
tnum = num;
num = num2;
num2 = tnum + num2;
}
}
here is the output i got:
*Please input a number:
9
0 1 1 2 3 5 8 13 21*

Write an application that inputs three integers from the user and displays the sum, average, product, smallest and largest of the numbers

I'm getting the sum, average and the product. The real difficulty I am facing is with the smallest and the largest number.
I can do it with two numbers, but three numbers is not making any sense to me. Ask me if my question isn't clear or if its not making sense.
import java.util.Scanner;
// exercise 2.17
public class ArithmeticSmallestLargest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num1;
int num2;
int num3;
int sum;
int average;
int product;
double largest
System.out.print("Enter First Integer: ");
num1 = input.nextInt();
System.out.print("Enter Second Integer: ");
num2 = input.nextInt();
System.out.print("Enter Third Integer: ");
num3 = input.nextInt();
sum = num1 + num2 + num3;
average = sum / 3;
product = num1 * num2 * num3;
if (largest =num1 > num2 & num2 > num3)
System.out.println(sum);
System.out.println(average);
System.out.println(product);
System.out.println("The biggest number is " + largest);
}
}
import java.util.Scanner;
// exercise 2.17
public class ArithmeticSmallestLargest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num1;
int num2;
int num3;
int sum;
int average;
int product;
int largest;
int smallest;
System.out.print("Enter First Integer: ");
num1 = input.nextInt();
System.out.print("Enter Second Integer: ");
num2 = input.nextInt();
System.out.print("Enter Third Integer: ");
num3 = input.nextInt();
sum = num1 + num2 + num3;
average = sum / 3;
product = num1 * num2 * num3;
largest = num1;
smallest = num1;
if(num2 > largest)
largest = num2;
if(num3 > largest)
largest = num3;
if(num2 < smallest)
smallest = num2;
if (num3 < smallest)
smallest = num3;
System.out.println("The sum is " + sum);
System.out.println("The average is " + average);
System.out.println("The product is " + product);
System.out.println("Largest of three integers is " + largest + " and the smallest is "+ smallest + ".");
}
}
import java.util.Scanner;
// exercise 2.17
public class ArithmeticSmallestLargest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num1;
int num2;
int num3;
int sum;
int average;
int product;
int largest;
int smallest;
System.out.print("Enter First Integer: ");
num1 = input.nextInt();
System.out.print("Enter Second Integer: ");
num2 = input.nextInt();
System.out.print("Enter Third Integer: ");
num3 = input.nextInt();
sum = num1 + num2 + num3;
average = sum / 3;
product = num1 * num2 * num3;
largest = num1;
smallest = num1;
if(num2 > largest)
largest = num2;
if(num3 > largest)
largest = num3;
if(num2 < smallest)
smallest = num2;
if (num3 < smallest)
smallest = num3;
System.out.println("The sum is " + sum);
System.out.println("The average is " + average);
System.out.println("The product is " + product);
System.out.println("Largest of three integers is " + largest + " and the smallest is "+ smallest + ".");
}
}
import java.util.Scanner;
public class ArithmeticSmallestLargest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num1;
int num2;
int num3;
int sum;
int average;
int product;
int largest;
int smallest;
System.out.print("Enter First Integer: ");
num1 = input.nextInt();
System.out.print("Enter Second Integer: ");
num2 = input.nextInt();
System.out.print("Enter Third Integer: ");
num3 = input.nextInt();
sum = num1 + num2 + num3;
average = sum / 3;
product = num1 * num2 * num3;
largest = num1;
smallest = num1;
if(num2 > largest)
largest = num2;
if(num3 > largest)
largest = num3;
if(num2 < smallest)
smallest = num2;
if (num3 < smallest)
smallest = num3;
System.out.printf("The sum is %d%n " , sum);
System.out.printf("The average is %d%n " , average);
System.out.printf("The product is %d%n " , product);
System.out.printf("Largest of three integers is %d%n " , largest);
System.out.printf("Smallest of three integers is %d%n " , smallest);
}
}
Answer is given below:
Enter First Integer: 60
Enter Second Integer: 90
Enter Third Integer: 30
The sum is 180
The average is 60
The product is 162000
Largest of three integers is 90
Smallest of three integers is 30
...Program finished with exit code 0
Press ENTER to exit console.

Categories