SecondsConverter.java:19:错误:不兼容的类型:String无法转换为int

时间:2016-10-06 02:15:25

标签: java

我必须为教科书练习做一个程序。预期目的是输入一个秒数,并将其转换为天,小时,分钟和秒。

import java.util.Scanner;
import static java.lang.System.*;

    public class SecondsConverter {

    public static void main(String[] args) {

       Scanner keyboard = new Scanner(System.in);

       System.out.print("Enter a number for seconds :: ");
       int input = keyboard.nextInt();

       int days = input/86400;
       int hours = (input%86400)/3600;
       int minutes = ((input%86400)/3600)/60;
       int seconds = ((input%86400)/3600)%60;

       System.out.println(days = " days, " + +hours + " hours, " + +minutes + " minutes, and " + +seconds + " seconds");

     }
}

以下是完整错误:

----jGRASP exec: javac -g SecondsConverter.java

SecondsConverter.java:19: error: incompatible types: String cannot be converted to int
System.out.println(days = " days, " + +hours + " hours, " + +minutes + " minutes, and " + +seconds + " seconds");
                                                                                                   ^
1 error

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

4 个答案:

答案 0 :(得分:1)

关于您的具体问题,这是System.out.println()来电中语法不正确的结果。

你有太多的加号运算符,你也使用了equals。你想要这样的东西:

System.out.println(days + " days, " +hours + " hours, " +minutes + " minutes, and " +seconds + " seconds");

作为旁注,当用户输入值60时,您的代码不起作用,它会打印" 0天,0小时,0分钟和0秒"

此外,您永远不会关闭Scanner。确保在完成后致电keyboard.close();

修改 以下项目是我想到的更多代码审查和一般增强功能,可以帮助您完成此编程和其他编程工作。由于这个程序的简单性,我认为这可能是一个很好的机会来说明一些重要的原则。

首先,考虑一下您在设计代码时如何测试代码。我建议您将业务逻辑(计算结果的部分)分成单独的方法。这将允许您调用方法并将自动输入传递给它,而不必手动键入值来测试它。以下是删除业务逻辑的示例:

public static void main(String[] args) {

   Scanner keyboard = new Scanner(System.in);

   System.out.print("Enter a number for seconds :: ");
   int input = keyboard.nextInt();

   if(input < 0){
       System.out.println("Sorry, negative values are not valid.");
   }else{
       SecondConversionResult result = calculate(input);

       System.out.println(result.getDays() + " days, " +result.getHours() + " hours, " +result.getMinutes() + " minutes, and " +result.getSeconds() + " seconds");
   }
 }

/**
 * Calculates the number of days, hours, minutes, and seconds represented by the specified number of seconds.
 * For example 3600 seconds is 1 hour.
 * @param input the number of seconds to convert to days, hours, minutes, and seconds 
 * @return an object representing the result of converting a number of seconds to number of days, hours, minutes, and seconds 
 */
public static SecondConversionResult calculate(int input){
    int seconds = ...
    int minutes = ...
    int hours = ...
    int days = ...

    return new SecondConversionResult(seconds, minutes, hours, days);
}

你会注意到这个我简单地称为calculate的新方法接受一个整数输入并产生一个表示结果的对象。这个对象是SecondConversionResult的一个实例,它是我创建的一个简单类,用于保存转换结果。该课程如下所示:

public class SecondConversionResult {
    private int seconds, minutes, hours, days;

    public SecondConversionResult(int sec, int min, int hrs, int days){
        seconds = sec;
        minutes = min;
        hours = hrs;
        this.days = days;
    }

    public int getSeconds() {
        return seconds;
    }

    public int getMinutes() {
        return minutes;
    }

    public int getHours() {
        return hours;
    }

    public int getDays() {
        return days;
    }
}

注意:在大多数情况下,我会为像这样的类创建setter方法,但由于我们在这里做的简单,我不认为这是非常必要的。< / p>

你可能想知道所有这些让我们做了什么。好了,既然我们有一个单独的方法可以传递输入并从中获取结果,我们可以编写一些非常简单的JUnit测试来确保我们的代码按预期工作。如果您不熟悉JUnit,可以阅读Getting Started Guide

例如,我们可以编写非常具体的测试:

import static org.junit.Assert.*;

import org.junit.Test;

public class JUnitTests {
    @Test
    public void test0() {
        SecondConversionResult result = Main.calculate(0);
        assertEquals(0, result.getDays());
        assertEquals(0, result.getHours());
        assertEquals(0, result.getMinutes());
        assertEquals(0, result.getSeconds());
    }
...

或者我们可以编写一个非常通用的测试,如下面的测试,测试从零秒到10天(864000秒)的所有值:

@Test
public void testAllUpTo10Days() {
    for(int days = 0; days < 10; days++){
        for(int hours = 0; hours < 24; hours++){
            for(int min = 0; min < 60; min++){
                for(int sec = 0; sec < 60; sec++){
                    int input = sec + min*60 + hours*60*60 + days*24*60*60;
                    System.out.println("Testing input of "+input);
                    SecondConversionResult result = Main.calculate(input);
                    assertEquals(days, result.getDays());
                    assertEquals(hours, result.getHours());
                    assertEquals(min, result.getMinutes());
                    assertEquals(sec, result.getSeconds());
                }
            }
        }
    }
}

现在我们有一种非常简单的方法可以确保我们的解决方案适用于我们需要它的任何场景。我希望这对你现在和将来都有所帮助。

答案 1 :(得分:0)

试试System.out.println(days + " days, " + hours + " hours, " + minutes + " minutes, and " + seconds + " seconds");。问题在于你从我能看到的内容中连接起来。

答案 2 :(得分:0)

System.out.println的格式错误

System.out.printf ("days = %d, hours = %d, minutes = %d, seconds = %d", 
                                                  days, hours, minutes, seconds);

答案 3 :(得分:0)

在System.out.println中,您输入等号而不是加号,以及其他多种语法错误。

System.out.println(days **=** " days, " + +hours + " hours, " + +minutes + " minutes, and " + +seconds + " seconds");

应该是

System.out.println(days + " days, " + hours + " hours, " + minutes + " minutes, and " + seconds + " seconds");