我刚刚看了一个名为“许多方法和实例”的 youtube 教程。他做了一个程序,你输入一些东西,上面写着“你的第一个女朋友是_ ”。但这太复杂了。首先是主类:
import java.util.Scanner;
public class MethodsInstances2 {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
MethodsInstances object = new MethodsInstances();
System.out.println("Enter name of first gf here: ");
String temp = input.nextLine();
object.setName(temp);
object.saying();
}
}
Run Code Online (Sandbox Code Playgroud)
接下来是它创建对象的类:
public class MethodsInstances {
private String girlName;
public void setName (String name){
girlName=name;
}
public String getName (){
return girlName;
}
public void saying(){
System.out.printf("Your first gf was %s", getName());
}
}
Run Code Online (Sandbox Code Playgroud)
考虑到我仍然是 Java 的新手,这似乎太复杂了,标题是我不明白的所有内容。这是我输入的速度提高了 4 倍的内容: …
我正在制作一个应该允许您输入文本的程序,它将在计算机上的每个字体中显示该文本.以下是加载字体时所发生情况的屏幕截图:
现在,我意识到除了JTextFields的自动调整大小之外还有十亿个其他问题,但我想一次关注一件事.无论如何,当我在JScrollPane中向下滚动时,会发生以下情况:
有人可以告诉我,我与GridBagConstraints或JTextFields有什么关系来解决这个问题?这是一些代码,希望它有所帮助 -
gbc.insets = new Insets(2, 5, 2, 5);
gbc.gridx = 0;
gbc.gridwidth = gbc.gridheight = 1;
gbc.weightx = gbc.weighty = 1;
gbc.anchor = GridBagConstraints.LINE_START;
Run Code Online (Sandbox Code Playgroud) 我刚开始学习代码(特别是Java),我正在测试一个密码系统,当你输入密码时,它变成变量"password",它会检查它是否等于password2,即实际密码.这是代码:
import java.util.Scanner;
public class LogicalOperators {
public static void main(String args[]){
Scanner test = new Scanner(System.in);
int age;
int password;
String password2;
password2 = "Call of Duty";
System.out.println("Please enter your age:");
age = test.nextInt();
if (age >=18) {
System.out.println("You are old enough.");
System.out.println("Please enter the password:");
password = test.nextInt();
if (password == password2) {
System.out.println("Welcome back!");
}else{
System.out.println("The password you typed was incorrect.");
}
}else{
System.out.println("You are too young.");
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试检查嵌套的if语句是否输入了匹配密码2,"使命召唤"的密码; 但问题是它不适用于字符串.这个问题的标题是出现的错误.有人可以帮帮我吗?
每当在类中声明main方法时,总是必须执行一个String名为"args" 的数组.重点是什么?除非我生活在摇滚之下,Java中的命令行语言几乎不再使用.当我尝试运行这个...
//this program won't compile
public class SomeClass {
public static void main(){
System.out.println("This text will never be displayed :(");
}
}
Run Code Online (Sandbox Code Playgroud)
输出以红色文本显示:
错误:在SomeClass类中找不到主方法,请将main方法定义为:
public static void main(String [] args)
我,新手Java程序员,如果有人告诉我为什么需要将该参数输入main方法,我将非常感激.
java convention parameters program-entry-point command-line-arguments
java ×4
convention ×1
if-statement ×1
instance ×1
int ×1
layout ×1
methods ×1
parameters ×1
printf ×1
string ×1
swing ×1