gpt4 book ai didi

java - Android Studio 预期为 'class' 或 'interface'

转载 作者:行者123 更新时间:2023-12-01 19:59:42 24 4
gpt4 key购买 nike

我用 Java 编写了一些代码来创建随机句子。它可以在在线 Java 编译器上运行,尽管我似乎无法让它在 Android Studio 上运行。这是我的代码:

package com.tech.littlest.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.Random;

public class Activity2 extends AppCompatActivity {
final static int NO_DEDICATED_ARRAYS = 12; // This number is equal to the number of arrays in a string NOTE: This number MUST be constant across all strings
final static int NO_OUTPUT_SENTENCES = 1; // How many sentences generated and displayed for the user
final static String SPACER = " "; // Inputs a " " character (Space) into the display
final static String PERIOD = ".";
static Random r = new Random();
public static void main( String args[] ){
String proposition_user[] = { "You should build and invention that", "You should build a robot that", "You should create a whatchamacallit that", "There should be a device that", "Create a futuristic thing that", "Make a thing that", "Make something cool that", "I don't recall there being a thing that", "Create a sick thing that", "Devise a plan to create an object that", "I want a thing that", "If I just had an thingamajig that", "Create a thingimajig that"};
String plural_noun[] = { "children", "chickens", "the ocean", "cars", "apples", "houses", "water", "buildings", "people", "your friends", "farms", "the human race" };
String verb[] = { "directs you to", "drives on", "manages", "develops", "makes","eats", "helps", "relocates", "fixes", "feeds", "runs on", "washes" };
String sentence;
for (int i = 0; i < NO_OUTPUT_SENTENCES; i++){
sentence = proposition_user[rand()];
char c = sentence.charAt(0);
sentence = sentence.replace( c, Character.toUpperCase(c) );
sentence += SPACER;
sentence += (verb[rand()]);
sentence += (SPACER + plural_noun[rand()]);
sentence += PERIOD;
sentence += "";
System.out.println(sentence);
}
}
static int rand(){
int ri = r.nextInt() % NO_DEDICATED_ARRAYS;
if ( ri < 0 )
ri += NO_DEDICATED_ARRAYS;
return ri;
}

}


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
}
}

我在第 37 行收到“类”或“接口(interface)”预期错误。倒数第三个括号。谢谢。

最佳答案

您应该删除 static int rand() 函数后面多余的 }

关于java - Android Studio 预期为 'class' 或 'interface',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48470531/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com