- 网页版App(利用HTML, CSS, JavaScript)fengbaoshun.com
前端(HTML + CSS + JavaScript)
index.html
html
Simple App Welcome to My App
Click Me!
- <script src="app.js"></script>
复制代码 styles.css css
body {
font-family: Arial, sans-serif;
text-align: center;
padding: 20px;
}
button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
app.js
javascript
document.getElementById(‘clickMe’).addEventListener(‘click’, function() {
document.getElementById(‘output’).innerHTML = ‘Hello, World!’;
});
2. Android App(利用Kotlin)
MainActivity.kt (Kotlin)
kotlin
package com.example.myapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
- val button: Button = findViewById(R.id.clickMe)
- val output: TextView = findViewById(R.id.output)
- button.setOnClickListener {
- output.text = "Hello, World!"
- }
- }
复制代码 }
activity_main.xml (Layout)
xml
<?xml version="1.0" encoding="utf-8"?>
- <TextView
- android:id="@+id/textView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Welcome to My App"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="20dp"
- android:textSize="24sp"/>
- <Button
- android:id="@+id/clickMe"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Click Me!"
- android:layout_centerHorizontal="true"
- android:layout_below="@id/textView"
- android:layout_marginTop="20dp"/>
- <TextView
- android:id="@+id/output"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text=""
- android:layout_centerHorizontal="true"
- android:layout_below="@id/clickMe"
- android:layout_marginTop="20dp"
- android:textSize="20sp"/>
复制代码 3. iOS App(利用Swift) ViewController.swift (Swift) swift import UIKit class ViewController: UIViewController {
- @IBOutlet weak var outputLabel: UILabel!
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view.
- }
- @IBAction func buttonTapped(_ sender: UIButton) {
复制代码 由于不同编程语言的特点和用途各异,这里我将以三种盛行的编程语言为例,分别提供一个简朴的“猜数字”游戏的小步调代码。这个游戏的根本逻辑是:步调随机生成一个数字,玩家实验推测这个数字,步调会告诉玩家推测的数字是太高、太低还是正确。
- Python 示例
python
import random
def guess_number_game():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
- print("我想了一个1到100之间的数字。你能猜到是哪个吗?")
- while guess != number_to_guess:
- try:
- guess = int(input("请输入你的猜测:"))
- attempts += 1
- if guess < number_to_guess:
- print("太低了!")
- elif guess > number_to_guess:
- print("太高了!")
- else:
- print(f"恭喜你!猜对了!数字就是{number_to_guess}。你尝试了{attempts}次。")
- except ValueError:
- print("请输入一个有效的数字。")
复制代码 guess_number_game()
2. JavaScript 示例(HTML 页面中的内嵌 JavaScript)
html
猜数字游戏 猜数字游戏
我有一个1到100之间的数字。你能猜到是哪个吗?
提交
- <script>
- let numberToGuess = Math.floor(Math.random() * 100) + 1;
- let guess = null;
- let attempts = 0;
- function checkGuess() {
- guess = parseInt(document.getElementById('guess').value);
- attempts++;
- if (isNaN(guess)) {
- document.getElementById('feedback').textContent = "请输入一个有效的数字。";
- return;
- }
- if (guess < numberToGuess) {
- document.getElementById('feedback').textContent = "太低了!";
- } else if (guess > numberToGuess) {
- document.getElementById('feedback').textContent = "太高了!";
- } else {
- document.getElementById('feedback').textContent = `恭喜你!猜对了!数字就是${numberToGuess}。你尝试了${attempts}次。`;
- }
- }
- </script>
复制代码 3. Java 示例(控制台应用) java import java.util.Random; import java.util.Scanner; public class GuessNumberGame {
public static void main(String[] args) {
Random rand = new Random();
int numberToGuess = rand.nextInt(100) + 1;
int guess = 0;
int attempts = 0;
- Scanner scanner = new Scanner(System.in);
- System.out.println("我想了一个1到100之间的数字。你能猜到是哪个吗?");
- while (guess != numberToGuess) {
- System.out.print("请输入你的猜测:");
-
- if (scanner.hasNextInt()) {
- guess = scanner.nextInt();
- attempts++;
- if (guess < numberToGuess) {
- System.out.println("太低了!");
- } else if (guess > numberToGuess) {
- System.out.println("太高了!");
- } else {
- System.out.println(String.format("恭喜你!猜对了!数字就是%d。你尝试了%d次。", numberToGuess, attempts));
- }
- } else {
- System.out.println("请输入一个有效的数字。");
- scanner.next(); // 清除输入缓冲区中的无效输入
- }
- }
- scanner.close();
- }
复制代码 }
这三个示例分别展示了怎样在 Python、JavaScript(在网页中)和 Java(控制台应用)中实现一个简朴的猜数字游戏。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |