System.out.println(false)的输出结果为什么是-1
时间: 2024-04-25 10:26:31 浏览: 14
不好意思,我的前面回答有误。System.out.println(false)的输出结果应该是false,而不是-1。我混淆了System.out.println(false)和System.out.printf("%d",false)的输出结果。对于System.out.printf("%d",false),因为%d表示输出整数,所以false会被转换成整数类型的0,而0对应的ASCII码是48,所以输出结果是48。
相关问题
public class WrapperTest { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int aa = sc.nextInt(); String bb = sc.next(); int c = sc.nextInt(); String str11 = sc.next(); String str22 = sc.next(); // 包装类中“==”与equals的用法比较 // 值得注意的是,包装类中的equals方法和String类一样,都是重写了Object类中的equals方法,因此比较的是内容而不是地址, // 而“==”比较的依然是引用变量的地址,只是当包装类型和与之相对应的基本类型进行“==”比较时会先做自动拆箱处理。 /********* Begin *********/ if(aa==bb) System.out.println("true"); else System.out.println("false"); if(aa==c) System.out.println("true"); else System.out.println("false"); if(bb==c) System.out.println("true"); else System.out.println("false"); if(aa.equals(bb)) System.out.println("true"); else System.out.println("false"); if(str11==str22) System.out.println("true"); else System.out.println("false"); if(str11.equals(str22)) System.out.println("true"); else System.out.println("false"); /********* End *********/ } }改正代码
代码中有几个错误,以下是改正后的代码:
```
public class WrapperTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int aa = sc.nextInt();
String bb = sc.next();
int c = sc.nextInt();
String str11 = sc.next();
String str22 = sc.next();
// 包装类中“==”与equals的用法比较
// 值得注意的是,包装类中的equals方法和String类一样,都是重写了Object类中的equals方法,因此比较的是内容而不是地址,
// 而“==”比较的依然是引用变量的地址,只是当包装类型和与之相对应的基本类型进行“==”比较时会先做自动拆箱处理。
/********* Begin *********/
if(aa==Integer.parseInt(bb)) // 需要将字符串转换为int类型再进行比较
System.out.println("true");
else
System.out.println("false");
if(aa==c)
System.out.println("true");
else
System.out.println("false");
if(Integer.parseInt(bb)==c) // 同样需要将字符串转换为int类型再进行比较
System.out.println("true");
else
System.out.println("false");
if(Integer.toString(aa).equals(bb)) // 需要将int类型转换为字符串再进行比较
System.out.println("true");
else
System.out.println("false");
if(str11==str22)
System.out.println("true");
else
System.out.println("false");
if(str11.equals(str22))
System.out.println("true");
else
System.out.println("false");
/********* End *********/
}
}
```
Java什么组件可以作为代码信息的实时输出结果,类似于System.out.println
在Java中,可以使用JTextArea组件作为代码信息的实时输出结果,类似于System.out.println。JTextArea是一个多行文本框,可以用于显示和编辑文本。
以下是一个简单的示例代码,演示如何使用JTextArea来实时输出代码信息:
```java
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class OutputWindow extends JFrame {
private JTextArea textArea;
public OutputWindow() {
setTitle("Output Window");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textArea = new JTextArea();
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
add(scrollPane);
}
public void appendText(String text) {
textArea.append(text + "\n");
textArea.setCaretPosition(textArea.getDocument().getLength());
}
}
```
在上述代码中,我们创建了一个名为OutputWindow的JFrame窗口,并在其中添加了一个JTextArea组件来显示输出信息。`appendText()`方法用于将文本追加到JTextArea中,并自动滚动到最后一行。
然后,在你的代码中,你可以通过创建OutputWindow实例,并在需要输出信息的地方调用`appendText()`方法来实时显示输出结果。
```java
public class Main {
public static void main(String[] args) {
OutputWindow outputWindow = new OutputWindow();
outputWindow.setVisible(true);
// 示例输出
outputWindow.appendText("Hello, World!");
outputWindow.appendText("This is a test message.");
}
}
```
当你运行上述代码时,你将看到输出窗口显示"Hello, World!"和"This is a test message.",这些信息是通过调用`appendText()`方法实时添加到JTextArea中的。
请注意,上述代码只是一个简单的示例,你可以根据你的需求进行扩展和修改。
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)