5 changed files with 57 additions and 0 deletions
@ -0,0 +1,23 @@ |
|||
# Q: |
|||
1.JAVA输入操作 |
|||
2.JAVA创建函数 |
|||
3.JAVA怎么输入比如36.6 C |
|||
4.sc.next().charAt(0)是什么意思? |
|||
# A: |
|||
1.最常用:Scanner 输入 |
|||
2.Java 里叫方法(Method),其实就是你说的函数 |
|||
修饰符 返回值类型 方法名(参数列表) { |
|||
// 代码 |
|||
return 返回值; |
|||
} |
|||
3.nextDouble() 读数字 |
|||
next().charAt(0) 读第一个字符(C/F) |
|||
4. sc.next() |
|||
|
|||
- 读取一个单词/一段不带空格的字符串 |
|||
- 比如你输入: C 或 F ,它就读到 "C" 、 "F" |
|||
|
|||
charAt(0) |
|||
|
|||
- 取这个字符串里第 0 个字符 |
|||
- 字符串的位置是 从 0 开始数 |
|||
@ -0,0 +1,5 @@ |
|||
public class Helloworld { |
|||
public static void main(String[]args){ |
|||
System.out.println("Hello world!"); |
|||
} |
|||
} |
|||
@ -0,0 +1 @@ |
|||
# |
|||
@ -0,0 +1,28 @@ |
|||
import java.util.Scanner;//使用Scanner类获取输入
|
|||
public class TemperatureConverter { |
|||
public static double celsius_to_fahrenheit(double c){ |
|||
return c*9.0/5.0+32.0; |
|||
} |
|||
//函数将摄氏度转换为华氏度。
|
|||
public static double fahrenheit_to_celsius(double f){ |
|||
return (f - 32.0) * 5.0 / 9.0; |
|||
} |
|||
//函数将华氏度转换为摄氏度。
|
|||
public static void main(String[]args){ |
|||
Scanner scan=new Scanner(System.in);//创建scan对象
|
|||
System.out.println("请输入要转换的温度和单位:(例如 36.6 C 或 97 F)"); |
|||
double temp=scan.nextDouble(); //输入双精度浮点数数值
|
|||
char unit=scan.next().charAt(0); //输入浮点数后面的字符C或F(温度或华氏度)
|
|||
if(unit =='C'){ |
|||
double f=celsius_to_fahrenheit(temp); |
|||
System.out.printf("%.1f ℃=%.2f ℉\n",temp,f); |
|||
} |
|||
//判断温度并输出华氏度
|
|||
if(unit =='F'){ |
|||
double c=fahrenheit_to_celsius(temp); |
|||
System.out.printf("%.1f ℉=%.2f ℃\n",temp,c); |
|||
} |
|||
//判断华氏度并输出温度
|
|||
scan.close(); |
|||
} |
|||
} |
|||
|
After Width: | Height: | Size: 409 KiB |
Loading…
Reference in new issue