Java实现好友验证代码
时间: 2023-11-12 21:04:57 浏览: 33
以下是 Java 实现的好友验证代码示例:
```java
import java.util.Scanner;
public class FriendVerification {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入你的姓名:");
String name = input.nextLine();
System.out.print("请输入你的年龄:");
int age = input.nextInt();
if (age < 18) {
System.out.println("对不起,您未满 18 岁,不能添加好友!");
} else {
System.out.println("欢迎添加好友!");
}
}
}
```
代码中,首先通过 Scanner 获取用户输入的姓名和年龄,然后根据年龄进行好友验证,如果年龄小于 18 岁,输出对应信息,否则输出欢迎信息。
相关问题
Java实现添加好友时发送验证消息的代码
以下是 Java 实现的添加好友时发送验证消息的代码示例:
```java
import java.util.Scanner;
public class FriendVerification {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入你的姓名:");
String name = input.nextLine();
System.out.print("请输入你的年龄:");
int age = input.nextInt();
if (age < 18) {
System.out.println("对不起,您未满 18 岁,不能添加好友!");
} else {
System.out.print("请输入验证消息:");
String message = input.next();
System.out.println("已发送验证消息:" + message);
}
}
}
```
代码中,当用户年龄大于等于 18 岁时,程序会要求用户输入验证消息,然后输出发送的验证消息。可以根据具体需求对代码进行调整。
我需要代码实现
好的,这里是一个简单的代码实现,仅供参考:
1. 在布局文件中添加登录页面的 UI 元素:
```xml
<EditText
android:id="@+id/usernameInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名" />
<EditText
android:id="@+id/passwordInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="密码" />
<Button
android:id="@+id/loginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录" />
```
2. 在代码中获取 UI 元素,并添加登录逻辑:
```java
public class LoginActivity extends AppCompatActivity {
private EditText mUsernameInput;
private EditText mPasswordInput;
private Button mLoginButton;
private SharedPreferences mSharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mSharedPreferences = getSharedPreferences("login", MODE_PRIVATE);
mUsernameInput = findViewById(R.id.usernameInput);
mPasswordInput = findViewById(R.id.passwordInput);
mLoginButton = findViewById(R.id.loginButton);
mLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = mUsernameInput.getText().toString();
String password = mPasswordInput.getText().toString();
// 在这里添加验证逻辑
if (username.equals("admin") && password.equals("123456")) {
// 登录成功,保存登录信息
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putBoolean("logged_in", true);
editor.putString("username", username);
editor.apply();
// 跳转到好友列表页面
Intent intent = new Intent(LoginActivity.this, FriendListActivity.class);
startActivity(intent);
finish();
} else {
// 登录失败,提示用户输入的用户名或密码错误
Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
protected void onResume() {
super.onResume();
// 检查登录状态,如果已经登录过,则直接跳转到好友列表页面
boolean loggedIn = mSharedPreferences.getBoolean("logged_in", false);
if (loggedIn) {
Intent intent = new Intent(LoginActivity.this, FriendListActivity.class);
startActivity(intent);
finish();
}
}
}
```
这段代码中,我们首先获取了用户名和密码的输入框以及登录按钮的 UI 元素,然后在登录按钮的点击事件中添加了验证逻辑。如果用户名和密码正确,则保存登录信息并跳转到好友列表页面;否则,提示用户输入的用户名或密码错误。
在 onResume 方法中,我们检查了登录状态,如果已经登录过,则直接跳转到好友列表页面,实现了自动登录的功能。
需要注意的是,这段代码仅供参考,实际应用中可能需要根据具体情况进行适当修改和完善。同时,为了保证用户的安全性,建议使用加密方式存储用户的密码信息。