将num转换为str,因为我想避免使用类 [英] Converting num to str as I want to avoid classes

查看:81
本文介绍了将num转换为str,因为我想避免使用类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java新手

嘿我的增强版if语句

如果有这个程序

如果有写时间我怎么转换从num到string,你可以看到它不能

写上AM并给出错误



我尝试了什么:



公共类Hello {



public static void main(String args []){< br $> b $ b

扫描仪bucky =新扫描仪(System.in);

int h,m,s,小时,迷你,秒,时间;

h = 28;

m = 33;

s = 55;

hour =((h> = 0&& ; h< 12)?h:0);

miniute =((m> = 0&& m< 60)?m:0);

second = ((s> = 0&& s< 60)?s:0);

time =((h< 12)?AM:PM);

System.out.printf(%02d:%02d:%02d%s,小时,小时,秒,时间);

}

}

New to Java
hey in my enhanced if statement
if have this programme
in which if have written time how do I convert from num to string as you can see it cannot
write AM and giving an error

What I have tried:

public class Hello {

public static void main(String args[]) {

Scanner bucky=new Scanner(System.in);
int h,m,s,hour,miniute,second,time;
h=28;
m=33;
s=55;
hour=((h>=0&&h<12)?h:0);
miniute=((m>=0&&m<60)?m:0);
second=((s>=0&&s<60)?s:0);
time=((h<12)?"AM":"PM");
System.out.printf("%02d:%02d:%02d%s",hour,miniute,second,time);
}
}

推荐答案

变量time被声明为int,但是你尝试分配一个文字字符串va Lue to it in this line:



The variable time is declared as int, but you try to assign a literal string value to it in this line:

time=((h<12)?"AM":"PM");





Java强制执行类型安全,不允许这样做。



Java enforces type safety and will not allow this.


这篇关于将num转换为str,因为我想避免使用类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆