import java.util.Scanner;
public class remainder
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
int a;
int b;
a=input.nextInt();
b=input.nextInt();
if(a%b!=0) System.out.printf("the first isn's a multiple of the second.");
else
System.out.printf("the first is a multiple of the second.");
}
} 主要程序是这样的`最后结果都是the first is a multiple of the second.无论输入什么数字`我很郁闷的啊`
然后用import java.util.Scanner;
public class remainder
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
int a;
int b;
System.out.printf("the first:");
a=input.nextInt();
System.out.printf("the second:");
b=input.nextInt();
if(a%b!=0) System.out.printf("the first isn's a multiple of the second.");
else
System.out.printf("the first is a multiple of the second.");
}
} 就又成功了 `这个就更刺激我了`前辈们`给点理论知识`我我不太懂了`
public class remainder
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
int a;
int b;
a=input.nextInt();
b=input.nextInt();
if(a%b!=0) System.out.printf("the first isn's a multiple of the second.");
else
System.out.printf("the first is a multiple of the second.");
}
} 主要程序是这样的`最后结果都是the first is a multiple of the second.无论输入什么数字`我很郁闷的啊`
然后用import java.util.Scanner;
public class remainder
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
int a;
int b;
System.out.printf("the first:");
a=input.nextInt();
System.out.printf("the second:");
b=input.nextInt();
if(a%b!=0) System.out.printf("the first isn's a multiple of the second.");
else
System.out.printf("the first is a multiple of the second.");
}
} 就又成功了 `这个就更刺激我了`前辈们`给点理论知识`我我不太懂了`
解决方案 »
- 各位帮忙看看吧 错在哪里。。。。
- 急急急!Timer类的定时问题
- 用了很久的JAVA,才发现main方法中的 String args[] 大部分时候是个摆设.
- 十万火急!在线等待
- 简单的变量问题~!困惑中!~望高手解答.谢谢~!
- ---java中如何textarea中的光标处插入文本-----
- 郁闷中!!!
- 如何设置tomcat,改变我的.class文件的默认路径。
- JAVA 2编程指南(JDK 1.3) --作者:美国佬
- 火狐浏览器使用firebug插件为什么HTML和firepath不显示代码内容
- 帮忙看下 谢谢
- public static void main(String args[]) ,不明白
Scanner input = new Scanner(System.in);
int a = 0; //给变量赋初值
int b = 0; //可避免很多问题
System.out.print("Please input the first number: ");
try {
a = Integer.parseInt(input.nextLine());
} catch (Exception e) {
System.out.println("Input error !");
}
System.out.print("Please input the second number: ");
try {
b = Integer.parseInt(input.nextLine());
} catch (Exception e) {
System.out.println("Input error !");
}
System.out.println("a = " + a); //用于验证输入的值到底是什么
System.out.println("b = " + b); //和上一语句都属调试性代码
不管我B输入什么值·都是等于A的值!·
首先要清楚一点,我们在控制台也就是CMD窗口中输入的只是字符序列,Scanner的功能是根据分割符(通常是空白符例如空格、制表符Tab、回车换行符等的序列)自动完成输入字符中词的分割,每两个空白符序列之间的字符会被认为是一个词,而nextInt的作用是将下一个的词解析为整数返回。
输入缓存(或称为缓冲区)的作用类似水库,存的是字符而已。当nextInt方法被调用时,首先它检测缓冲区是否为空,如果不为空则根本不会出现闪动的光标提示输入,而是直接从缓冲区中读出下一个词,并将其从缓冲区中删除,然后作解析并返回解析出的整数。
当然刚刚初始化好的Scanner的缓冲区是空的,这时当nextInt(或其它nextXXX方法)被调用时,由于缓冲区是空的没有字符可读,所以程序暂停执行(更准确的叫法是main线程被阻塞)出现闪动的光标,这时我们就可以输入字符了。输入的字符会自动存入上面提到的缓冲区,当我们键入回车时,暂停执行的程序会恢复执行,下面所做的事儿和上面相同,即:从缓冲区中读出下一个词,并将其从缓冲区中删除,然后作解析并返回解析出的整数。
原理如上,仔细分析一下你会发现问题出在哪儿的。
只是第二段在从键盘捕获 int 型的 a 和 b 的值的时候多了两句提示用的语句而已如果你输入的a和b的值一样 余数肯定是0啊 是不是你自己输入的过程中有笔误啊