'How can I fix my IDE, which puts lots of squiggle lines over my Java code?
I just started to learn Java using Eclipse. My current homework asks me to write codes where users can choose two games: game 1 is converting seconds value to the "hour:minute:second" format; game 2 is to convert input into the sum of individual digits.
My coding is below, but I can't run it, as it contains lots of problem, such as 'selection does not contain main type', 'cannot convert from int to boolean', among many others. I tried to correct them on Eclipse and also searched the solutions on Stack Overflow, but cannot resolve it.
import java.util.Scanner;
public class SimpleGame {
/**
* Write a method to convert the given seconds to hours:minutes:seconds.
* @param seconds to convert
* @return string for the converted seconds in the format: 23:59:59
*
* Example(s):
* - If input seconds is 1432, print and return output in the format: 0:23:52
* - If input seconds is 0, print and return output in the format: 0:0:0
* - If input seconds is not valid (negative), print and return: -1:-1:-1.
* So if input seconds is -2, print and return: -1:-1:-1
* If input seconds is -3214, likewise print and return: -1:-1:-1
*/
public String convertTime(int seconds){
// TODO: Your code goes here
if (seconds == 0) {
return System.out.println("0:0:0");
if (seconds < 0) {
return System.out.println("-1:-1:-1");
if (seconds > 0) {
int hour = seconds/3600;
int min = (seconds%3600))/60;
int sec = seconds % 60;
String strhour = Integer.toString(hour);
String strmin = Integer.toString(min);
String strsec = Integer.toString(sec);
return System.out.println(strhour + ":" + strmin + ":" + strsec);
}
}
}
}
/**
* Write a method that adds all the digits in the given non-negative integer.
*
* @param integer to add digits
* @return integer in which all the digits in the given non-negative integer are
* added.
*
* Example(s): - If input is 565, print and return 16. - If input is 7,
* print and return 7. - If input is 0, print and return 0.
*/
public int digitsSum(int input) {
// TODO: Your code goes here
return 0;
}
public static void main(String[] args) {
// Create an instance of the SimpleGame class.
// TODO: Your code goes here
Scanner sc = new Scanner(System.in);
// Ask the user which game to play.
System.out.println("which game to play?");
// Then ask the user for input and pass the value to the corresponding method.
String game = sc.next();
// If the user enters 1, ask for an integer to convert and call the convertTime
// method.
if (game == "1") {
System.out.println("what's your integer?");
int num1 = sc.next();
sc.convertTime(num1);
}
// If the user enters 2, ask for an integer and call the digitsSum method.
if (game == "2") {
System.out.println("what's your integer?");
int num2 = sc.next();
sc.digitsSum(num2);
}
// TODO: Your code goes here
sc.close();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
