Few problems I'm having with Java at the moment, would be grateful if any of you could give me a hand.
Wrote this piece of code to input 2 numbers from user, square root of 1, and cube root of another. Program works but I want to know while using the Scanner class is there a way so that I can get the user to input multiple data on one line, instead of the program moving to the next line after each input.
package Assignment2;
import java.util.Scanner;
import java.text.DecimalFormat;
public class CubeSquare {
public static void main(String[] args)
{
double number1, number2, min;
Scanner scan = new Scanner (System.in);
DecimalFormat fmt1 = new DecimalFormat ("00.###");
System.out.println("Enter 2 numbers:");
number1 = scan.nextDouble();
number2 = scan.nextDouble();
number1 = Math.cbrt(number1);
number2 = Math.abs(number2);
number2 = Math.sqrt(number2);
min = Math.min(number1,number2);
System.out.println(""+ fmt1.format(min));
}
}
Another program i'm having problems with is that I have written this for loop program to input 2 numbers into a system and add up the range inclusively, for example inputting 10 and 14 would give you 10+11+12+13+14 = 60, I need it so that it will work when the user enters a second number that is smaller than the first like 10 and 3. The program doesn't seem to work when that is the outcome at the moment.
package Assignment2;
import java.util.Scanner;
public class Ranger {
public static void main(String[] args) {
int number1,number2,count,totalsum =0;
Scanner scan = new Scanner (System.in);
System.out.println("Please enter two integers:");
number1 = scan.nextInt();
number2 = scan.nextInt();
for (count=number1; count<=number2; count++)
{
totalsum = totalsum+count;
}
System.out.println(""+totalsum);
}
}
Thank you.