Famcraft
General Category => General Discussion => Topic started by: eGI on April 10, 2014, 10:09:04 PM
-
Runner.java
public class Runner
{
String Runner1, Runner2, Runner3;
private double Time1;
private double Time2;
private double Time3;
public void setRunner1(String r1)
{
Runner1 = r1;
}
public void setRunner2(String r2)
{
Runner2 = r2;
}
public void setRunner3(String r3)
{
Runner3 = r3;
}
public String getRunner1()
{
return Runner1;
}
public String getRunner2()
{
return Runner2;
}
public String getRunner3()
{
return Runner3;
}
public double getTime1()
{
return Time1;
}
public double getTime2()
{
return Time2;
}
public double getTime3()
{
return Time3;
}
}
RunnerDemo.java
import java.util.Scanner;
public class RunnerDemo
{
public static void main(String[] args)
{
String Runner1, Runner2, Runner3;
double Time1;
double Time2;
double Time3;
int status = -1;
Scanner keyboard = new Scanner(System.in);
System.out.print("What is the name of the first runner? ");
Runner1 = keyboard.nextLine();
System.out.print("What is the name of the second runner? ");
Runner2 = keyboard.nextLine();
System.out.print("What is the name of the third runner? ");
Runner3 = keyboard.nextLine();
System.out.print("What was the time for " + Runner1 + "? ");
Time1 = keyboard.nextDouble();
System.out.print("What was the time for " + Runner2 + "? ");
Time2 = keyboard.nextDouble();
System.out.print("What was the time for " + Runner3 + "? ");
Time3 = keyboard.nextDouble();
If statements go here to order stuff to order input from quickest time to slowest time
}
}
-
Runner.java
public class Runner
{
String runner; \\remember the tradition of variables always being lowercase
private double time;
public Runner(String r, double t)
{
runner = r;
time = t;
}
public String getRunner()
{
return runner;
}
public double getTime()
{
return time;
}
}
RunnerDemo.java
import java.util.Scanner;
public class RunnerDemo
{
public static void main(String[] args)
{
String runner1, runner2, runner3;
double time1;
double time2;
double time3;
int status = -1;
Scanner keyboard = new Scanner(System.in);
System.out.print("What is the name of the first runner? ");
runner1 = keyboard.nextLine();
System.out.print("What is the name of the second runner? ");
runner2 = keyboard.nextLine();
System.out.print("What is the name of the third runner? ");
runner3 = keyboard.nextLine();
System.out.print("What was the time for " + Runner1 + "? ");
time1 = keyboard.nextDouble();
System.out.print("What was the time for " + Runner2 + "? ");
time2 = keyboard.nextDouble();
System.out.print("What was the time for " + Runner3 + "? ");
time3 = keyboard.nextDouble();
Runner run1 = new Runner(runner1, time1);
Runner run2 = new Runner(runner2, time2);
Runner run3 = new Runner(runner3, time3);
if (run1.getTime() > run2.getTime() && run1.getTime() > run3.getTime())
{
System.out.println(run1.getRunner());
if (run2.getTime() > run3.getTime())
System.out.println(run2.getRunner());
else
System.out.println(run3.getRunner());
}
if (run2.getTime() > run1.getTime() && run2.getTime() > run3.getTime())
{
System.out.println(run2.getRunner());
if (run1.getTime() > run3.getTime())
System.out.println(run1.getRunner());
else
System.out.println(run3.getRunner());
}
if (run3.getTime() > run1.getTime() && run3.getTime() > run2.getTime())
{
System.out.println(run3.getRunner());
if (run1.getTime() > run2.getTime())
System.out.println(run1.getRunner());
else
System.out.println(run2.getRunner());
}
}
}
This, or some variant, should work. I will note that there are a few easier ways using ArrayLists, but I don't know whether or not you have gotten that far into learning java yet, so I used the brute force method with the if statements. I hoped this helped! ^^
-
import java.util.Scanner;
public class RunnerDemo
{
public static void main(String[] args)
{
String runner1, runner2, runner3;
double time1;
double time2;
double time3;
Scanner keyboard = new Scanner(System.in);
System.out.print("What is the name of the first runner? ");
runner1 = keyboard.nextLine();
System.out.print("What is the name of the second runner? ");
runner2 = keyboard.nextLine();
System.out.print("What is the name of the third runner? ");
runner3 = keyboard.nextLine();
System.out.print("What was the time for " + runner1 + "? ");
time1 = keyboard.nextDouble();
System.out.print("What was the time for " + runner2 + "? ");
time2 = keyboard.nextDouble();
System.out.print("What was the time for " + runner3 + "? ");
time3 = keyboard.nextDouble();
Runner run1 = new Runner(runner1, time1);
Runner run2 = new Runner(runner2, time2);
Runner run3 = new Runner(runner3, time3);
if (run1.getTime() > run2.getTime() && run1.getTime() > run3.getTime())
{
System.out.println(run1.getRunner() + " was in first with a time of " + run1.getTime());
if (run2.getTime() > run3.getTime())
System.out.println(run2.getRunner() + " was in second with a time of " + run2.getTime());
System.out.println(run3.getRunner() + " was in last with a time of " + run3.getTime());
else if (run2.getTime() < run3.getTime())
System.out.println(run3.getRunner() + " was in second with a time of " + run3.getTime());
System.out.println(run2.getRunner() + " was in last with a time of " + run2.getTime());
}
if (run2.getTime() > run1.getTime() && run2.getTime() > run3.getTime())
{
System.out.println(run2.getRunner() + " was in first with a time of " + run2.getTime());
if (run1.getTime() > run3.getTime())
System.out.println(run1.getRunner() + " was in second with a time of " + run1.getTime());
System.out.println(run3.getRunner() + " was in last with a time of " + run3.getTime());
else if (run1.getTime() < run3.getTime())
System.out.println(run3.getRunner() + " was in second with a time of " + run3.getTime());
System.out.println(run1.getRunner() + " was in last with a time of " + run1.getTime());
}
if (run3.getTime() > run1.getTime() && run3.getTime() > run2.getTime())
{
System.out.println(run3.getRunner() + " was in first with a time of " + run3.getTime());
if (run1.getTime() > run2.getTime())
System.out.println(run1.getRunner() + " was in second with a time of " + run1.getTime());
System.out.println(run2.getRunner() + " was in last with a time of " + run2.getTime());
else if (run1.getTime() < run2.getTime())
System.out.println(run2.getRunner() + " was in second with a time of " + run2.getTime());
System.out.println(run1.getRunner() + " was in last with a time of " + run1.getTime());
}
}
}
Getting the 'else' without 'if' error
-
import java.util.Scanner;
public class RunnerDemo
{
public static void main(String[] args)
{
String runner1, runner2, runner3;
double time1;
double time2;
double time3;
Scanner keyboard = new Scanner(System.in);
System.out.print("What is the name of the first runner? ");
runner1 = keyboard.nextLine();
System.out.print("What is the name of the second runner? ");
runner2 = keyboard.nextLine();
System.out.print("What is the name of the third runner? ");
runner3 = keyboard.nextLine();
System.out.print("What was the time for " + runner1 + "? ");
time1 = keyboard.nextDouble();
System.out.print("What was the time for " + runner2 + "? ");
time2 = keyboard.nextDouble();
System.out.print("What was the time for " + runner3 + "? ");
time3 = keyboard.nextDouble();
Runner run1 = new Runner(runner1, time1);
Runner run2 = new Runner(runner2, time2);
Runner run3 = new Runner(runner3, time3);
if (run1.getTime() > run2.getTime() && run1.getTime() > run3.getTime())
{
System.out.println(run1.getRunner() + " was in first with a time of " + run1.getTime());
if (run2.getTime() > run3.getTime())
{
System.out.println(run2.getRunner() + " was in second with a time of " + run2.getTime());
System.out.println(run3.getRunner() + " was in last with a time of " + run3.getTime());
}
else
System.out.println(run3.getRunner() + " was in second with a time of " + run3.getTime());
System.out.println(run2.getRunner() + " was in last with a time of " + run2.getTime());
}
}
if (run2.getTime() > run1.getTime() && run2.getTime() > run3.getTime())
{
System.out.println(run2.getRunner() + " was in first with a time of " + run2.getTime());
if (run1.getTime() > run3.getTime())
{
System.out.println(run1.getRunner() + " was in second with a time of " + run1.getTime());
System.out.println(run3.getRunner() + " was in last with a time of " + run3.getTime());
}
else
{
System.out.println(run3.getRunner() + " was in second with a time of " + run3.getTime());
System.out.println(run1.getRunner() + " was in last with a time of " + run1.getTime());
}
}
if (run3.getTime() > run1.getTime() && run3.getTime() > run2.getTime())
{
System.out.println(run3.getRunner() + " was in first with a time of " + run3.getTime());
if (run1.getTime() > run2.getTime())
{
System.out.println(run1.getRunner() + " was in second with a time of " + run1.getTime());
System.out.println(run2.getRunner() + " was in last with a time of " + run2.getTime());
}
else
{
System.out.println(run2.getRunner() + " was in second with a time of " + run2.getTime());
System.out.println(run1.getRunner() + " was in last with a time of " + run1.getTime());
}
}
}
}
-
Woohoo... everything is fine now... I even finally realized I had > instead of < and had things ordered from slowest to fastest instead of the other way around.
-
Perfect! I'm glad everything is making sense to you now! :D
-
i suggest using C#, and i dont know java :P