所以,伙计们,我正在通过电报机器人API通过Java做电报长轮询机器人。 我做了一个整数测试,在得到良好答案之后做+1而没有整数,而答案是错误的。
public void onUpdateReceived(Update update) {
int i=0;
在更新接收的最开始。 当用户开始测试时,他会看到带有第一个问题和4个答案的标记键盘
else if (message_text.equals("test"))
{
SendMessage message = new SendMessage() // Create a message object object
.setChatId(chat_id)
.setText("Test");
// Create ReplyKeyboardMarkup object
ReplyKeyboardMarkup keyboardMarkup = new ReplyKeyboardMarkup();
// Create the keyboard (list of keyboard rows)
List<KeyboardRow> keyboard = new ArrayList<>();
// Create a keyboard row
KeyboardRow row = new KeyboardRow();
// Set each button, you can also use KeyboardButton objects if you need something else than text
row.add("1. М");
row.add("2. end");
// Add the first row to the keyboard
keyboard.add(row);
// Create another keyboard row
row = new KeyboardRow();
row.add("3. К");
row.add("4. Т");
keyboard.add(row);
// Set the keyboard to the markup
keyboardMarkup.setKeyboard(keyboard);
// Add it to the message
message.setReplyMarkup(keyboardMarkup);
try
{
sendMessage(message); // Call method to send the photo
}
catch (TelegramApiException e)
{
e.printStackTrace();
}
}
else if (message_text.equals("1. М"))
{
i=i+1;
SendMessage message = new SendMessage() // Create a message object object
.setChatId(chat_id)
.setText("Test");
ReplyKeyboardMarkup keyboardMarkup = new ReplyKeyboardMarkup();
List<KeyboardRow> keyboard = new ArrayList<>();
KeyboardRow row = new KeyboardRow();
row.add("1. М");
row.add("2. end");
keyboard.add(row);
row = new KeyboardRow();
row.add("3. К");
row.add("4. Т");
keyboard.add(row);
keyboardMarkup.setKeyboard(keyboard);
message.setReplyMarkup(keyboardMarkup);
try
{
sendMessage(message);
}
catch (TelegramApiException e)
{
e.printStackTrace();
}
}
else if (message_text.equals("2. end"))
{
if (i == 1) {
SendMessage message = new SendMessage()
.setChatId(chat_id)
.setText("roflan");
try {
sendMessage(message);
} catch (TelegramApiException e) {
e.printStackTrace();
}
}
}
如果我写“!= 1”,在“2.结束”之后它总是显示结果。用户回答第一个按钮的次数无关紧要,i + 1不起作用。我的逻辑问题出在哪里?
答案 0 :(得分:1)
让我们忘记Telegram API只是为了观看int i
变量发生的事情。
每次调用onUpdateReceived()
时,int i
都会在此方法中声明并使用值0
进行初始化。
看起来像这样
public class Scope {
public static void main(String[] args) {
System.out.println(getI());
System.out.println(getI());
}
private static int getI() {
int i = 0;
i++;
return i;
}
}
输出
1
1
要使您的计划按预期工作,您应在int i
范围之外声明onUpdateReceived()
。最明显的方法是创建一个静态变量。
public class Scope {
public static void main(String[] args) {
System.out.println(getI());
System.out.println(getI());
System.out.println(getI());
System.out.println(getI());
}
private static int i = 0;
private static int getI() {
i++;
return i;
}
}
输出
1
2
3
4
所以,现在你的代码看起来应该是这样的
public class Bot extends TelegramLongPollingBot {
private static int i = 0;
public void onUpdateReceived() {
/*...*/
else if (update.hasMessage() && update.getMessage().hasText() && update.getMessage().getText().equals("1. M")) {
i++;
} else if (update.hasMessage() && update.getMessage().hasText() && update.getMessage().getText().equals("1. end")) {
System.out.println(i);
}
/*...*/
}
public String getBotToken() {
return "...";
}
public String getBotUsername() {
return "...";
}
}