import java.io.*;
import java.lang.Math.*;
class task1
{
public static void main(String args[])throws Exception
{
BufferedReader input=
new BufferedReader(new InputStreamReader(System.in));
// our double
double a = 0.0;
double b = 0.0;
double c = 0.0;
// a boolean on which we will loop
boolean correct = false;
do {
System.out.print("a: ");
String inputStr = input.readLine();
try {
a = Double.parseDouble(inputStr);
// parse worked flag to end loop
correct = true;
}
catch(NumberFormatException e)
{
System.out.println("Please enter a number");
}
System.out.print("b: ");
inputStr = input.readLine();
try {
b = Double.parseDouble(inputStr);
// parse worked flag to end loop
correct = true;
}
catch(NumberFormatException e)
{
System.out.println("Please enter a number");
}
System.out.print("c: ");
inputStr = input.readLine();
try {
c = Double.parseDouble(inputStr);
// parse worked flag to end loop
correct = true;
}
catch(NumberFormatException e)
{
System.out.println("Please enter a number");
}
} while(!correct);
double temp=Math.sqrt((b*b)-(4*a*c));
double temp2=(-b+temp)/2*a;
System.out.println("The first solution is "+temp2);
double temp1=Math.sqrt((b*b)-(4*a*c));
double temp3=(-b-temp1)/2*a;
System.out.println("The second solution is "+temp3);
}
}
My program runs when user input value for a,b and c
when i input a word in example a = b it will prompt "please enter a numbe"r afterward it will skip A and move to b. So how do i write out a code to reprint it to made sure when user after enter a word it will go back to A and ask ask user to enter a number hope i did no make ur blur
example:
a = 2
b= b
Please enter a number
"then it jump to c and will not go back to b to enter a number"
c= -4
The First solution is .....
the Second solution is ......
1 last questions
import java.io.*;
// starting point of program
class smooth
{
// starting point of application
public static void main(String[] args)
{
int[] signal = {1, 5, 4, 5, 7, 6, 8, 6, 5, 4, 5, 4};
int[] smooth = makeItSmooth(signal);
for(int i = 0;i<smooth.length;i++){
System.out.print(" "+smooth[i]);
}
}
public static int[] makeItSmooth(int[] signal){
int[] result = new int[signal.length];
for(int i =0;i<signal.length;i++){
if(i==0){
result[0] = (signal[0]+signal[1])/2;
}
if(i==signal.length-1){
result[signal.length-1]=(signal[signal.length-1]+signal[signal.length-2])/2;
break;
}
if(i>0){
result[i] = (signal[i]+signal[i+1]+signal[i-1])/3;
}
}
return result;
}
}
is there any way i can shorten the code?

New Topic/Question
Reply



MultiQuote





|